how to use the object property of NSNotificationcenter
Solution 1
The object
parameter represents the sender of the notification, which is usually self
.
If you wish to pass along extra information, you need to use the NSNotificationCenter
method postNotificationName:object:userInfo:
, which takes an arbitrary dictionary of values (that you are free to define). The contents needs to be actual NSObject
instances, not an integral type such as an integer, so you need to wrap the integer values with NSNumber
objects.
NSDictionary* dict = [NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:index]
forKey:@"index"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"myevent"
object:self
userInfo:dict];
Solution 2
The object
property is not appropriate for that. Instead you want to use the userinfo
parameter:
+ (id)notificationWithName:(NSString *)aName
object:(id)anObject
userInfo:(NSDictionary *)userInfo
userInfo
is, as you can see, an NSDictionary specifically for sending information along with the notification.
Your dispatchFunction
method would instead be something like this:
- (void) disptachFunction:(int) index {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:index] forKey:@"pass"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"myevent" object:nil userInfo:userInfo];
}
Your receiveEvent
method would be something like this:
- (void)receiveEvent:(NSNotification *)notification {
int pass = [[[notification userInfo] valueForKey:@"pass"] intValue];
}
dubbeat
Updated on July 05, 2022Comments
-
dubbeat almost 2 years
Could somebody please show me how to use the object property on NSNotifcationCenter. I want to be able to use it to pass an integer value to my selector method.
This is how I have set up the notification listener in my UI View. Seeing as I want an integer value to be passed I'm not sure what to replace nil with.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent:) name:@"myevent" object:nil]; - (void)receiveEvent:(NSNotification *)notification { // handle event NSLog(@"got event %@", notification); }
I dispatch the notification from another class like this. The function is passed a variable named index. It's this value that I want to somehow fire off with the notification.
-(void) disptachFunction:(int) index { int pass= (int)index; [[NSNotificationCenter defaultCenter] postNotificationName:@"myevent" object:pass]; //[[NSNotificationCenter defaultCenter] postNotificationName:<#(NSString *)aName#> object:<#(id)anObject#> }
-
dubbeat over 13 yearsI went with your option. The only extra thing I had to do was to wrap an autorelease pool around the dictionary.
-
gavinb over 13 years(Oh, and my solution is the same as Matthew's, I just managed to click Submit a little quicker!)
-
gavinb over 13 yearsWhen you say "wrap an autorelease pool around the dictionary", do you mean you added a call to autorelease the dict, or you created an actual NSAutoreleasePool object? The latter is not necessary unless you are on a secondary thread, and the former is not necessary for class initialisers of this form, as it is not init or new.
-
Selvin about 11 years"The object property is not appropriate for that." Why its not appropriate? Anyway if I try to use object property to pass (for example) an NSString*. What will happen?
-
Matthew Frederick about 11 years@Selvin It's for sending the object posting the notification (you'd set it to
self
if you wanted to use it). What will happen if you put something else there? I have no idea, but if I had to guess, it could mess up things going on under the cover, like Notification Center tracking what needs to be released. Why risk it when there's a actual system for passing objects around? -
max almost 11 years"An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification—typically the object that posted the notification itself. The dictionary may contain additional information about the event." Sounds to me like it would be perfectly fine to put something other than
self
andnil
inobject:
. -
Matthew Frederick over 9 years@mdorseif "typically the object that posted the notification itself" is why it's not the best place. It's where you put the caller, generally. Won't break anything, but is non-standard, so why do it when there's a perfectly good place to put the data? Why make your future self or some other poor soul wonder why the caller's
self
isn't in the object where it belongs, instead of some other random data. -
Sinisa Drpa over 7 yearsAlso be advised of the following when you're setting object: "The name and object parameters of both methods are used to decide whether the criteria of a posted notification match the observer. If name is set, only notifications with that name will trigger, but if nil is set, then all names will match. The same is true of object. So, if both name and object are set, only notifications with that name and the specified object will trigger. However, if both name and object are nil, then all notifications posted will trigger." - nshipster.com/nsnotification-and-nsnotificationcenter
-
SomaMan over 3 yearsWe've pretty much always used the object parameter for sending the data, as the receiver of the notification has absolutely no interest in the sender, and there's no coupling between sender & receiver (ie, there's no need for the receiver to know anything about keys in an arbitrary userInfo dictionary). I believe that in the more modern world of Swift & type safety, dictionaries of this sort are an anachronism - maybe there's a case for more modern, type-safe notifications based on the Notification Name?