Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use parametrized method with NSNotificationCenter?

I'd like to pass dict to the method processit. But once I access the dictionary, I get 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];

In the recipient method:

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

Any suggestions on what I'm doing wrong?

like image 642
4thSpace Avatar asked Jun 23 '09 21:06

4thSpace


1 Answers

You will receive an NSNotification object, not an NSDictionary in the notification callback.

Try this:

- (void) processit: (NSNotification *)note {
    NSString *test = [[note userInfo] valueForKey:@"l"];
    NSLog(@"output is %@", test);
}
like image 129
amrox Avatar answered Oct 25 '22 02:10

amrox