How to use parametrized method with NSNotificationCenter?


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?


Memory management and performSelectorInBackground:

1:

tableView footer strange behavior
You will receive an NSNotification object, not an NSDictionary in the notification callback. Truly modal UIAlertView in iPhone? . MVC and the communication between them. What did i miss? Try this:. Accessing GPS on Blackberry using Javascript and sending lat/long values to a server via url
- (void) processit: (NSNotification *)note {     NSString *test = [[note userInfo] valueForKey:@"l"];     NSLog(@"output is %@", test); } 
What is something you wish you had known sooner about the iPhone SDK?How do I center a UIImageView within a full-screen UIScrollView?

2:

SSLHandshakeException unknown_ca apns java
Amrox is absolutely right.. One must also use Object (instead of userInfo) for the same as below: .
- (void) processit: (NSNotification *)note {      NSDictionary *dict = (NSDictionary*)note.object;      NSString *test = [dict valueForKey:@"l"];     NSLog(@"output is %@", test); } 
In this case your postNotificationName:object will look like:.
[[NSNotificationCenter defaultCenter] postNotificationName:@"atest" object:dict]; 

3:

You will receive an NSNotification object, not an NSDictionary in the notification callback..
  • (void) processit: (NSNotification *)note {. NSDictionary dict = (NSDictionary)note.object;. NSString *test = [dict valueForKey:@"l"];. NSLog(@"output is %@", test); }.


94 out of 100 based on 59 user ratings 454 reviews

*