7

メソッド processit に dict を渡したいと思います。しかし、辞書にアクセスすると、EXC__BAD_INSTRUCTION が表示されます。

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);
}

私が間違っていることについて何か提案はありますか?

4

3 に答える 3

17

通知コールバックの NSDictionary ではなく、NSNotification オブジェクトを受け取ります。

これを試して:

- (void) processit: (NSNotification *)note {
    NSString *test = [[note userInfo] valueForKey:@"l"];
    NSLog(@"output is %@", test);
}
于 2009-06-23T21:34:06.550 に答える
2

Amroxは絶対に正しいです。

以下のように、(userInfo の代わりに) Object を使用することもできます。

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

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

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

この場合、postNotificationName:object は次のようになります。

[[NSNotificationCenter defaultCenter] postNotificationName:@"atest" object:dict];
于 2011-05-19T06:43:33.440 に答える