2009-06-23 11 views
7

私はメソッドprocessitにdictを渡したいと思います。しかし、辞書にアクセスすると、EXC__BAD_INSTRUCTIONを取得します。受信者の方法ではNSNotificationCenterでパラメータ化されたメソッドを使用するには?

NSNotificationCenter *ncObserver = [NSNotificationCenter defaultCenter]; 
[ncObserver addObserver:self selector:@selector(processit:) name:@"atest" 
       object:nil]; 

NSDictionary *dict = [[NSDictionary alloc] 
          initWithObjectsAndKeys:@"testing", @"first", nil]; 
NSString *test = [dict valueForKey:@"first"]; 
NSNotificationCenter *ncSubject = [NSNotificationCenter defaultCenter]; 
[ncSubject postNotificationName:@"atest" object:self userInfo:dict]; 

:私が間違ってやっているの

- (void) processit: (NSDictionary *)name{ 
    NSString *test = [name valueForKey:@"l"]; //EXC_BAD_INSTRUCTION occurs here 
    NSLog(@"output is %@", test); 
} 

任意の提案ですか?

答えて

17

通知コールバックにNSDictionaryではなくNSNotificationオブジェクトが表示されます。

これを試してみてください:

- (void) processit: (NSNotification *)note { 
    NSString *test = [[note userInfo] valueForKey:@"l"]; 
    NSLog(@"output is %@", test); 
} 
2

Amroxが絶対的に正しいです。

一つも以下のように同じのために(代わりのUserInfoの)オブジェクトを使用することができます。

- (void) processit: (NSNotification *)note { 

    NSDictionary *dict = (NSDictionary*)note.object; 

    NSString *test = [dict valueForKey:@"l"]; 
    NSLog(@"output is %@", test); 
} 

この場合、あなたのpostNotificationNameを:あなたはNSNotificationを受け取ることになります

[[NSNotificationCenter defaultCenter] postNotificationName:@"atest" object:dict]; 
+0

ありがとうございます。私は次回からも書式を守ります。 :) –

0

:オブジェクトは、次のようになります通知コールバックのNSDictionaryではなく、オブジェクトです。

  • (ボイド)processit:(NSNotification *)注{

    NSDictionaryののdict =(NSDictionaryの)note.object。

    NSString * test = [dict valueForKey:@ "l"];

    NSLog(@ "output is%@"、test); }

関連する問題