What's Difference between NSNotification 'object' and 'userInfo'?
Asked Answered
O

3

7

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?

Othilie answered 16/2, 2015 at 10:2 Comment(2)
If you look at the doc of userInfos of NSNotification you'll see that object is the notification sender, and userInfos contains additional info.Pringle
But object also can contains additional info. Is there difference?Othilie
H
3

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.

Hydrophobia answered 16/2, 2015 at 10:9 Comment(1)
Thanks, but I wanna know that the "danger" if I use object to send additional info?Othilie
R
3

If you define an object you can filter the notifications sent only by that object. For instance if you register for a notification, specifying an object as notificationSender you get notification only from that object even if the notification name is the same for other posted notifications:

- (void)addObserver:(id)notificationObserver
       selector:(SEL)notificationSelector
           name:(NSString *)notificationName
         object:(id)notificationSender

Here is from Apple doc:

notificationSender The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.

If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

Rale answered 16/2, 2015 at 10:7 Comment(0)
H
3

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.

Hydrophobia answered 16/2, 2015 at 10:9 Comment(1)
Thanks, but I wanna know that the "danger" if I use object to send additional info?Othilie
H
0

When working with an NSNotification object, you’ll want to familiarize yourself the userInfo dictionary, which provides access to any additional objects that may be of interest to the receiver. Understanding the object method may also be helpful if you are using the same notification on more than one object.

For Further detail go thtough this link.

http://iosdevelopertips.com/cocoa/nsnotification-userinfo-and-object-methods.html

Heflin answered 16/2, 2015 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.