您的位置:首页 > 移动开发 > IOS开发

iOS中Notification的官方文档解读

2016-05-06 23:13 369 查看


NSNotification



左边-NSNotification  继承自NSObject  中间 这个类遵循的协议是NSCoding,NSCopying,NSObject.重要的申明:Founndation框架
------------------------------------

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对象是不可变的。


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.
这些通知的对象使用指针做比较是否和本地通知相等。发布通知使用字符串作为他们的对象,这些字符串使用isEqual方法,因为指针相等,跨越边界时并讲不通。


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
4000
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方法会抛异常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: