如何在NSNotificationCenter中使用参数化方法?

4th*_*ace 7 iphone cocoa-touch objective-c nsnotifications

我想将dict传递给方法processit.但是一旦我访问字典,我就得到了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];
Run Code Online (Sandbox Code Playgroud)

在收件人方法中:

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

关于我做错的任何建议?

amr*_*rox 17

您将收到NSNotification对象,而不是通知回调中的NSDictionary.

试试这个:

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