What's difference between NSNotification's object and userInfo?
When I post a notification with a parameter, I can use object or userInfos to do it. But I do not know what the difference is between these two ways.
Is there some advantages to using userInfo? Or is using object enough?
A structure that defines the name of a notification.
A notification dispatch mechanism that enables the broadcast of information to registered observers.
It uses the Observer pattern to inform registered observers when a notification comes in, using a central dispatcher called NotificationCenter. Before we get started, some quick notes: The NotificationCenter class used to be called NSNotificationCenter – with the “NS” prefix – prior to Swift 3.
addObserver(self, — This is for the class where we are going to observer notification. selector: #selector(loginSuccess) — This is the method name, whenever notification will receive this method call. name: NSNotification.Name(“com.
The object represent the object which posted the notification. userInfo contains the additional information/data for the receiving object/function.
According to NSNotificationCenter Class Reference:
postNotificationName:object:userInfo:Creates a notification with a given name, sender, and information and posts it to the receiver.
Declaration
Swift
func postNotificationName(_ notificationName: String, object notificationSender: AnyObject?, userInfo userInfo: [NSObject : AnyObject]?)Objective-C
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfoParameters
notificationNameThe name of the notification.
notificationSenderThe object posting the notification.
userInfoInformation about the the notification. May be nil.
Discussion
This method is the preferred method for posting notifications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With