NSNotification 'object' 和 'userInfo' 有什么区别?
What's Difference between NSNotification 'object' and 'userInfo'?
NSNotification
的 object
和 userInfo
有什么区别?
当我 post 一个带有参数的通知时,我可以使用 object
或 userInfos
来完成它。但是我不知道这两种方式有什么区别。
使用 userInfo
有什么优势吗?或者使用 object
就够了吗?
如果您定义一个对象,您可以过滤仅由该对象发送的通知。例如,如果您注册一个通知,将一个对象指定为 notificationSender
,即使通知名称与其他发布的通知相同,您也只能从该对象收到通知:
- (void)addObserver:(id)notificationObserver
selector:(SEL)notificationSelector
name:(NSString *)notificationName
object:(id)notificationSender
这里来自 Apple 文档:
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.
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.
有关详细信息,请阅读此 link。
http://iosdevelopertips.com/cocoa/nsnotification-userinfo-and-object-methods.html
object
表示发布通知的对象。 userInfo
包含用于接收 object/function 的附加 information/data。
根据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.
NSNotification
的 object
和 userInfo
有什么区别?
当我 post 一个带有参数的通知时,我可以使用 object
或 userInfos
来完成它。但是我不知道这两种方式有什么区别。
使用 userInfo
有什么优势吗?或者使用 object
就够了吗?
如果您定义一个对象,您可以过滤仅由该对象发送的通知。例如,如果您注册一个通知,将一个对象指定为 notificationSender
,即使通知名称与其他发布的通知相同,您也只能从该对象收到通知:
- (void)addObserver:(id)notificationObserver
selector:(SEL)notificationSelector
name:(NSString *)notificationName
object:(id)notificationSender
这里来自 Apple 文档:
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.
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.
有关详细信息,请阅读此 link。
http://iosdevelopertips.com/cocoa/nsnotification-userinfo-and-object-methods.html
object
表示发布通知的对象。 userInfo
包含用于接收 object/function 的附加 information/data。
根据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.