I am writting an SDK and looking for the best practise to send an object via NSNotification
.
Apple and some threads including this indicate that the object
param in postNotificationNamer
should be the sender of the notification, which in most case, self
. And your custom object should be passed via userInfo NSDictionary
. From Apple doc:
Creates a notification with a given name, sender, and information and posts it to the receiver.
For example, the inappropriate practise is:
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification"
object:myObject];
And the recommended way is:
NSDictionary* userInfo = @{@"myMessage": myObject};
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification"
object:self
userInfo:userInfo];
I have tried both methods and they all work well. Question is, is there any risk to use former method? In my case I am not interested in the sender of the notification, but using the latter method introduces an additional wrapper (NSDicionary
) around the actual object I want to send.