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 NSNotificationto
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
NSNotificationand
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].
NSNotificationis
not meant to be instantiated directly, and its
initmethod
raises an exception.
创建子类
你可依创建一个类继承自NSNotification对象包含名字,对象,和字典等信息。这个额外的数据必须是发布者和监听者约定的数据。
NSNotification是一个没有实例变量的类。就这点而论,你必须继承自NSNotification并且重写这些基本方法,name,object,userInfo.你能选择任意你喜欢的自定义指定构造器,但是必须要注意的是你不能调用super
init 方法。NSNotification不意味着能被直接实例化,它的init方法会抛异常。
相关文章推荐
- ios 3D引擎 SceneKit 开发(4) --关于旋转的几点问题(1)
- iOS 应用程序的启动过程
- 学习iOS
- 学习iOS【3】数组、词典和集合
- IOS开发中使用CNContact\CNMutableContact 对通讯录增删改查
- iOS之FMDB的基本使用
- iOS开发之原生二维码生成
- iOS之数据加密
- iOS数据解析之JSON解析
- 深拷贝、浅拷贝
- iOS之数据解析之XML解析
- 自定义相册
- github如何上传iOS源代码(最精炼版)
- iOS 开发查看应用的沙盒文件
- IOS自动释放原理及详解
- iOS获取各种文件目录路径的方法
- iOS 沙盒目录结构介绍
- iOS源代码管理工具
- iOS9 新变动
- iOS之多线程