OC中的KVO

来源:互联网 发布:淘宝有艾希情人节cdk吗 编辑:程序博客网 时间:2024/06/06 01:59
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//  ViewController.m</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//  OC-08_02</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//  Created by Ibokan on 15/12/18.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//  Copyright © 2015<span style="line-height: normal; font-family: 'Heiti SC Light';">年</span> ibokan. All rights reserved.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #822e0e">#import </span>"ViewController.h"</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(130, 46, 14);">#import <span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">"Hero.h"</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #822e0e">#import </span>"Observe.h"</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(60, 130, 140);"><span style="font-variant-ligatures: no-common-ligatures; color: #c32275">@interface</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>ViewController<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> ()</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);">@end</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #c32275">@implementation</span> ViewController</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #3c828c">Hero</span> *hero;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #3c828c">Observe</span> *observe;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">- (<span style="font-variant-ligatures: no-common-ligatures; color: #c32275">void</span>)viewDidLoad {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [</span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275">super</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>viewDidLoad<span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span>//KVO</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #1d9421">/*</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: 'Heiti SC Light'; color: rgb(29, 148, 33);"><span style="line-height: normal; font-family: Menlo;">     KVO</span>的由来:在编程过程中,我们经常需要判断目标是否发生变化,以便及时的做出对应的处理,此时苹果公司就提供了一种策略,既<span style="line-height: normal; font-family: Menlo;">‘OC</span>运行时<span style="line-height: normal; font-family: Menlo;">’</span>提供<span style="line-height: normal; font-family: Menlo;">‘KVO’</span>技术,其中<span style="line-height: normal; font-family: Menlo;">kvo</span>是基于<span style="line-height: normal; font-family: Menlo;">kvc</span>实现</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33); min-height: 24px;">     </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">     KVO<span style="line-height: normal; font-family: 'Heiti SC Light';">的实现</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">     1.<span style="line-height: normal; font-family: 'Heiti SC Light';">注册成观察者</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">     2.<span style="line-height: normal; font-family: 'Heiti SC Light';">观察者定义</span>KVO<span style="line-height: normal; font-family: 'Heiti SC Light';">回调</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">     3.<span style="line-height: normal; font-family: 'Heiti SC Light';">移除观察者</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">     */</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #539aa4">hero</span> = [<span style="font-variant-ligatures: no-common-ligatures; color: #3c828c">Hero</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">new</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [</span><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4">hero</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>setValue<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">@100</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>forKey<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"_HP"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #539aa4">observe</span> = [[<span style="font-variant-ligatures: no-common-ligatures; color: #3c828c">Observe</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">alloc</span>] <span style="font-variant-ligatures: no-common-ligatures; color: #294c50">initWithHero</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #539aa4">hero</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [</span><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4">hero</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>setValue<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">@300</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>forKey<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"_HP"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [</span><span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSTimer</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>scheduledTimerWithTimeInterval<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">1.0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>target<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>selector<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275">@selector</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">(change:) </span>userInfo<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>repeats<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275">YES</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: 'Heiti SC Light'; color: rgb(29, 148, 33);"><span style="line-height: normal; font-family: Menlo; color: rgb(0, 0, 0);">    </span><span style="line-height: normal; font-family: Menlo;">//</span>所以如果在主线程中可以不用写<span style="line-height: normal; font-family: Menlo;">NSRunLoop</span>作运行环境的语句,但是如果在非主线程中就要将<span style="line-height: normal; font-family: Menlo;">NSTimer</span>加入到该线程的<span style="line-height: normal; font-family: Menlo;">NSRunLoop</span>中,并启动<span style="line-height: normal; font-family: Menlo;">NSRunLoop</span>才行。</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span>//[[NSRunLoop currentRunLoop] run];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">(</span>@"main RunLoop =%@"<span style="font-variant-ligatures: no-common-ligatures; color: #000000">,[</span><span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSRunLoop</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">mainRunLoop</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">]);</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">-(<span style="font-variant-ligatures: no-common-ligatures; color: #c32275">void</span>)change:(<span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSTimer</span> *)timer</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSUInteger</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> _curHP = [[</span><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4">hero</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>valueForKey<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"_HP"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">] </span>integerValue<span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    _curHP -= <span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">50</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #c32275">if</span> (_curHP <= <span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">0</span>) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">        [<span style="font-variant-ligatures: no-common-ligatures; color: #539aa4">hero</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">setValue</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">@0</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">forKey</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"_HP"</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">        [timer <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">invalidate</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #c32275">else</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">        [</span><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4">hero</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>setValue<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:[</span><span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSNumber</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>numberWithInteger<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:_curHP] </span>forKey<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"_HP"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">- (<span style="font-variant-ligatures: no-common-ligatures; color: #c32275">void</span>)didReceiveMemoryWarning {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [</span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275">super</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>didReceiveMemoryWarning<span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span>// Dispose of any resources that can be recreated.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);">@end</p>

创建一个Hero类,定义一个实例变量_HP

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//  Hero.h</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//  OC-08_02</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//  Created by Ibokan on 15/12/18.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//  Copyright © 2015<span style="line-height: normal; font-family: 'Heiti SC Light';">年</span> ibokan. All rights reserved.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);">//</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #822e0e">#import </span><Foundation/Foundation.h></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);">@interface<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> Hero : </span><span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSObject</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSInteger</span> _HP;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; min-height: 24px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);">@end</p>

定义一个观察者类Observe

Observe的.h文件

//

//  Observe.h

//  OC-08_02

//

//  Created by Ibokan on 15/12/18.

//  Copyright © 2015 ibokan. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Hero.h"

@interface Observe : NSObject


@property (nonatomic,retain)Hero *hero;


-(id)initWithHero:(Hero *)hero;


@end


Observe的.m文件

//

//  Observe.m

//  OC-08_02

//

//  Created by Ibokan on 15/12/18.

//  Copyright © 2015 ibokan. All rights reserved.

//


#import "Observe.h"


@implementation Observe


-(id)initWithHero:(Hero *)hero

{

    if (self = [superinit]) {

        self.hero = hero;

        //注册监听者

        /*

         NSKeyValueObservingOptionNew

         NSKeyValueObservingOptionOld

         NSKeyValueObservingOptionInitial

         NSKeyValueObservingOptionPrior

         */

        [self.heroaddObserver:selfforKeyPath:@"_HP"options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:nil];

    }

    return self;

}


//定义KVO回调

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context

{

    NSLog(@"change = %@",change [NSKeyValueChangeNewKey]);

    

    /*

     change 是一个字典 包含的Key有如下

     

     NSKeyValueChangeKindKey

     NSKeyValueChangeNewKey    //新值

     NSKeyValueChangeOldKey    //旧值

     NSKeyValueChangeIndexesKey

     NSKeyValueChangeNotificationIsPriorKey

     */

}


//移除监听者

-(void)dealloc

{

    /*

     NSObject类定义了一个方法叫dealloc,当对象没有拥有者并且内存要被回收时,系统会自动调用dealloc方法。dealloc的责任是释放对象自己的内存,释放所有它控制的资源,包括释放所有实例变量的所有权

     */

    [self.heroremoveObserver:selfforKeyPath:@"_HP"];

    NSLog(@"I am die");


}



@end




0 0
原创粉丝点击