iOS中Notification的官方文件解讀
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 or . However, you don’t usually create your own notifications directly. The NSNotificationCenter methods and allow you to conveniently post a notification without creating it first.
翻譯:NSNotification物件囊括了一些資訊,以便它能被NSNotificationCenter廣播出去給其他物件。一個NSNotification物件(作為notification例項被NSNotification引用),包括了一個通知的名稱,通知的物件,和一個可選值字典.(因為字典是可選,所以不傳遞引數時可以直接寫nil).這個名稱是一個通知的標籤。這個物件時通知的釋出者,想要傳遞給監聽者一個通知(典型的案例就是,這個物件就是已經發布的通知)。這個字典儲存一些相關的物件,有必要的話。NSNotification物件是不可變的。
Object Comparison
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.
Creating Subclasses
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
, ,
and .
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方法會拋異常。