Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's Difference between NSNotification 'object' and 'userInfo'?

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?

like image 265
saitjr Avatar asked Feb 16 '15 10:02

saitjr


People also ask

What is NSNotification name?

A structure that defines the name of a notification.

What is NSNotificationCenter in IOS?

A notification dispatch mechanism that enables the broadcast of information to registered observers.

What is notification observer in IOS?

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.

How do I add an observer in Swift?

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.


1 Answers

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 *)userInfo

Parameters

notificationName

The name of the notification.

notificationSender

The object posting the notification.

userInfo

Information about the the notification. May be nil.

Discussion

This method is the preferred method for posting notifications.

like image 176
Midhun MP Avatar answered Oct 25 '22 00:10

Midhun MP