NSKeyValueObservingOptions详解

options是KVO中常见的参数,然而通常只是将它固定为“NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld”来使用却不知道到底这个参数有什么用。由此本文通过一个例子来描述该参数的实际表现。


NSKeyValueObservingOptions是options的类型(

- (void)addObserver:(NSObject *)observer

             forKeyPath:(NSString *)keyPath

                 options:(NSKeyValueObservingOptions)options

                  context:(void *)context


首先看头文件中的定义:

typedefNS_OPTIONS(NSUInteger, NSKeyValueObservingOptions) {

/* Whether the change dictionaries sent in notifications should contain NSKeyValueChangeNewKey and NSKeyValueChangeOldKey entries, respectively.

*/

NSKeyValueObservingOptionNew =0x01,

NSKeyValueObservingOptionOld =0x02,

/* Whether a notification should be sent to the observer immediately, before the observer registration method even returns. The change dictionary in the notification will always contain an NSKeyValueChangeNewKey entry if NSKeyValueObservingOptionNew is also specified but will never contain an NSKeyValueChangeOldKey entry. (In an initial notification the current value of the observed property may be old, but it's new to the observer.) You can use this option instead of explicitly invoking, at the same time, code that is also invoked by the observer's -observeValueForKeyPath:ofObject:change:context: method. When this option is used with -addObserver:toObjectsAtIndexes:forKeyPath:options:context: a notification will be sent for each indexed object to which the observer is being added.

*/

NSKeyValueObservingOptionInitial NS_ENUM_AVAILABLE(10_5,2_0) =0x04,

/* Whether separate notifications should be sent to the observer before and after each change, instead of a single notification after the change. The change dictionary in a notification sent before a change always contains an NSKeyValueChangeNotificationIsPriorKey entry whose value is [NSNumber numberWithBool:YES], but never contains an NSKeyValueChangeNewKey entry. You can use this option when the observer's own KVO-compliance requires it to invoke one of the -willChange... methods for one of its own properties, and the value of that property depends on the value of the observed object's property. (In that situation it's too late to easily invoke -willChange... properly in response to receiving an -observeValueForKeyPath:ofObject:change:context: message after the change.)

When this option is specified, the change dictionary in a notification sent after a change contains the same entries that it would contain if this option were not specified, except for ordered unique to-many relationships represented by NSOrderedSets.For those, for NSKeyValueChangeInsertion and NSKeyValueChangeReplacement changes, the change dictionary for a will-change notification contains an NSKeyValueChangeIndexesKey (and NSKeyValueChangeOldKey in the case of Replacement where the NSKeyValueObservingOptionOld option was specified at registration time) which give the indexes (and objects) which *may* be changed by the operation.The second notification, after the change, contains entries reporting what did actually change.For NSKeyValueChangeRemoval changes, removals by index are precise.

*/

NSKeyValueObservingOptionPrior NS_ENUM_AVAILABLE(10_5,2_0) =0x08

};

可以很清楚的看到,其中包含了四种值,分别为:

NSKeyValueObservingOptionNew:提供更改前的值

NSKeyValueObservingOptionOld:提供更改后的值

NSKeyValueObservingOptionInitial:观察最初的值(在注册观察服务时会调用一次触发方法)

NSKeyValueObservingOptionPrior:分别在值修改前后触发方法(即一次修改有两次触发)


以下是一个简单的测试案例:


测试代码

将options修改为NSKeyValueObservingOptionNew得到测试结果:

2016-07-04 19:18:29.265 testForKVO[50887:35316085] turn1

2016-07-04 19:18:29.265 testForKVO[50887:35316085]    kind: 1

2016-07-04 19:18:29.266 testForKVO[50887:35316085]    new: turn1

2016-07-04 19:18:29.266 testForKVO[50887:35316085] turn2

2016-07-04 19:18:29.266 testForKVO[50887:35316085]    kind: 1

2016-07-04 19:18:29.266 testForKVO[50887:35316085]    new: turn2

可以看到观察被触发2次,在change对象中包含key为new的值,并且为stringA的最新值


将options修改为NSKeyValueObservingOptionOld得到测试结果:

2016-07-04 19:19:14.562 testForKVO[50912:35317211] turn1

2016-07-04 19:19:14.562 testForKVO[50912:35317211]    kind: 1

2016-07-04 19:19:14.562 testForKVO[50912:35317211]    old: turn0

2016-07-04 19:19:14.563 testForKVO[50912:35317211] turn2

2016-07-04 19:19:14.563 testForKVO[50912:35317211]    kind: 1

2016-07-04 19:19:14.563 testForKVO[50912:35317211]    old: turn1

可以看到观察被触发2次,在change对象中包含key为old的值,并且为stringA被修改前的值


将options修改为NSKeyValueObservingOptionInitial得到测试结果:

//注册观察服务时

2016-07-04 19:20:08.003 testForKVO[50938:35318482] turn0

2016-07-04 19:20:08.004 testForKVO[50938:35318482]    kind: 1

//self.stringA=@"turn1";

2016-07-04 19:20:08.004 testForKVO[50938:35318482] turn1

2016-07-04 19:20:08.004 testForKVO[50938:35318482]    kind: 1

//self.stringA=@"turn2";

2016-07-04 19:20:08.004 testForKVO[50938:35318482] turn2

2016-07-04 19:20:08.005 testForKVO[50938:35318482]    kind: 1

可以看到观察被触发3次,在观察服务注册时,执行了一次触发


将options修改为NSKeyValueObservingOptionPrior得到测试结果:

2016-07-04 19:20:47.936 testForKVO[50962:35319521] turn0

2016-07-04 19:20:47.937 testForKVO[50962:35319521]    kind: 1

2016-07-04 19:20:47.937 testForKVO[50962:35319521]    notificationIsPrior: 1

2016-07-04 19:20:47.937 testForKVO[50962:35319521] turn1

2016-07-04 19:20:47.937 testForKVO[50962:35319521]    kind: 1

2016-07-04 19:20:47.937 testForKVO[50962:35319521] turn1

2016-07-04 19:20:47.938 testForKVO[50962:35319521]    kind: 1

2016-07-04 19:20:47.938 testForKVO[50962:35319521]    notificationIsPrior: 1

2016-07-04 19:20:47.938 testForKVO[50962:35319521] turn2

2016-07-04 19:20:47.938 testForKVO[50962:35319521]    kind: 1

可以看到观察被触发4次,从turn0->turn1改变前后触发2次,turn1->turn2改变前后触发2次


其它


将options修改为 0 得到测试结果:

2016-07-04 19:50:43.925 testForKVO[51293:35351701] turn1

2016-07-04 19:50:43.925 testForKVO[51293:35351701]    kind: 1

2016-07-04 19:50:43.925 testForKVO[51293:35351701] turn2

2016-07-04 19:50:43.925 testForKVO[51293:35351701]    kind: 1


将options修改为

NSKeyValueObservingOptionNew|

NSKeyValueObservingOptionOld|

NSKeyValueObservingOptionInitial|

NSKeyValueObservingOptionPrior得到测试结果:

//注册观察服务时

2016-07-04 19:33:12.326 testForKVO[51133:35333070] turn0

2016-07-04 19:33:12.326 testForKVO[51133:35333070]    kind: 1

2016-07-04 19:33:12.326 testForKVO[51133:35333070]    new: turn0

//self.stringA=@"turn1";

2016-07-04 19:33:12.327 testForKVO[51133:35333070] turn0

2016-07-04 19:33:12.327 testForKVO[51133:35333070]    kind: 1

2016-07-04 19:33:12.327 testForKVO[51133:35333070]    old: turn0

2016-07-04 19:33:12.327 testForKVO[51133:35333070]    notificationIsPrior: 1

2016-07-04 19:33:12.327 testForKVO[51133:35333070] turn1

2016-07-04 19:33:12.327 testForKVO[51133:35333070]    kind: 1

2016-07-04 19:33:12.327 testForKVO[51133:35333070]    old: turn0

2016-07-04 19:33:12.327 testForKVO[51133:35333070]    new: turn1

//self.stringA=@"turn2";

2016-07-04 19:33:12.328 testForKVO[51133:35333070] turn1

2016-07-04 19:33:12.328 testForKVO[51133:35333070]    kind: 1

2016-07-04 19:33:12.328 testForKVO[51133:35333070]    old: turn1

2016-07-04 19:33:12.328 testForKVO[51133:35333070]    notificationIsPrior: 1

2016-07-04 19:33:12.328 testForKVO[51133:35333070] turn2

2016-07-04 19:33:12.328 testForKVO[51133:35333070]    kind: 1

2016-07-04 19:33:12.328 testForKVO[51133:35333070]    old: turn1

2016-07-04 19:33:12.328 testForKVO[51133:35333070]    new: turn2


参考资料:

ios里的KVO模式


00000002

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,384评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,845评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,148评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,640评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,731评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,712评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,703评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,473评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,915评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,227评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,384评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,063评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,706评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,302评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,531评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,321评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,248评论 2 352

推荐阅读更多精彩内容