NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object. An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable objects.
You can create a notification object with the class methods notificationWithName:object:
or notificationWithName:object:userInfo:
. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods postNotificationName:object:
and postNotificationName:object:userInfo:
allow you to conveniently post a notification without creating it first.
翻译:NSNotification对象囊括了一些信息,以便它能被NSNotificationCenter广播出去給其他对象。一个NSNotification对象(作为notification实例被NSNotification引用),包括了一个通知的名称,通知的对象,和一个可选值字典.(因为字典是可选,所以不传递参数时可以直接写nil).这个名称是一个通知的标签。这个对象时通知的发布者,想要传递給监听者一个通知(典型的案例就是,这个对象就是已经发布的通知)。这个字典存储一些相关的对象,有必要的话。NSNotification对象是不可变的。
The objects of a notification are compared using pointer equality for local notifications. Distributed notifications use strings as their objects, and those strings are compared using isEqual:
because pointer equality doesn’t make sense across process boundaries.
You can subclass NSNotification
to contain information in addition to the notification name, object, and dictionary. This extra data must be agreed upon between notifiers and observers.
NSNotification is a class cluster with no instance variables. As such, you must subclass NSNotification
and override the primitive methods name
, object
, and userInfo
. You can choose any designated initializer you like, but be sure that your initializer does not call [super init]
. NSNotification
is not meant to be instantiated directly, and its init
method raises an exception.
你可依创建一个类继承自NSNotification对象包含名字,对象,和字典等信息。这个额外的数据必须是发布者和监听者约定的数据。
NSNotification是一个没有实例变量的类。就这点而论,你必须继承自NSNotification并且重写这些基本方法,name,object,userInfo.你能选择任意你喜欢的自定义指定构造器,但是必须要注意的是你不能调用super init 方法。NSNotification不意味着能被直接实例化,它的init方法会抛异常。