10个迷惑新手的Cocoa&Objective-c开发问题

文章转自:http://lianxu.me/blog/2011/11/14/10-cocoa-objc-newbie-problems/
  1. language background
  2. run­time
  3. thread
  4. run­loop
  5. del­e­gate, protocol
  6. event respon­der
  7. mem­ory management
  8. class heritage and category
  9. design pattern
  10. drawing issue

首先请谅解我可能使用很多英文,毕竟英文资料将来会是你的主要资料来源。

这篇博客将描述一些我见到的众多Cocoa开发新手遇到的问题和障碍。在你继续深入学习MacOS编程之前,请停下脚步弄清这些问题。如果你是新手,这个教程不要希望一次能看的非常透彻,学一定阶段反回来再看看又会有新的体会的。

1. language background

首先c, c++语言背景,必须。 很多人问:”没有任何语言基础,我不想学c直接学objective-c。“ 这里我简单说几句,objc是c的超集,也就是说大部分objc代码其实是c、而且众多开源代码是c,c++写成的。你不学好c在unix世界里只能是个二流开发者!也许说得过于严厉,不过自己斟酌把。

接着English,必须。 苹果不会把它们文档都写成中文的。“什么,有人翻译?” 等有人闲着翻译出来了的时候,大家都已经卖了很多软件了。你也是跟着人家屁股后面做开发。

2. runtime(运行时)

Objective-c是动态语言, 很多新手或者开发人员常常被runtime这个东西所迷惑。而恰恰这是一个非常重要的概念。我可以这么问:“如果让你(设计、)实现一个计算机语言,你要如何下手?” 很少程序员这么思考过。但是这么一问,就会强迫你从更高层次思考1以前的问题了。 注意我这句话‘设计’括起来了,稍微次要点,关键是实现。

我把实现分成3种不同的层次:

第一种是传统的面向过程的语言开发,例如c语言。实现c语言编译器很简单,只要按照语法规则实现一个LALR语法分析器就可以了,编译器优化是非常难的topic,不在这里讨论范围内,忽略。 这里我们实现了编译器其中最最基础和原始的目标之一就是把一份代码里的函数名称,转化成一个相对内存地址,把调用这个函数的语句转换成一个jmp跳转指令。在程序开始运行时候,调用语句可以正确跳转到对应的函数地址。 这样很好,也很直白,但是太死板了。Everything is predetermined.

我们希望语言更加灵活,于是有了第二种改进,开发面向对象的语言,例如c++。 c++在c的基础上增加了类的部分。但这到底意味着什么呢?我们再写它的编译器要如何考虑呢?其实,就是让编译器多绕个弯,在严格的c编译器上增加一层类处理的机制,把一个函数限制在它处在的class环境里,每次请求一个函数调用,先找到它的对象, 其类型,返回值,参数等等,确定了这些后再jmp跳转到需要的函数。这样很多程序增加了灵活性同样一个函数调用会根据请求参数和类的环境返回完全不同的结果。增加类机制后,就模拟了现实世界的抽象模式,不同的对象有不同的属性和方法。同样的方法,不同的类有不同的行为! 这里大家就可以看到作为一个编译器开发者都做了哪些进一步的思考。虽然面相对象语言有所改进,但还是死板, 我们仍然叫c++是static language.

希望更加灵活!于是我们完全把上面哪个类的实现部分抽象出来,做成一套完整运行阶段的检测环境,形成第三种,动态语言。这次再写编译器甚至保留部分代码里的sytax名称,名称错误检测,runtime环境注册所以全局的类,函数,变量等等信息等等,我们可以无限的为这个层增加必要的功能。调用函数时候,会先从这个运行时环境里检测所以可能的参数再做jmp跳转。这,就是runtime。编译器开发起来比上面更加弯弯绕。但是这个层极大增加了程序的灵活性。 例如当调用一个函数时候,前2种语言,很有可能一个jmp到了一个非法地址导致程序crash, 但是在这个层次里面,runtime就过滤掉了这些可能性。 这就是为什么dynamic langauge更加强壮。 因为编译器和runtime环境开发人员已经帮你处理了这些问题。

好了上面说着这么多,我们再返回来看objective-c的这些语句:

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">10</span>
id obj=self; if ([obj respondsToSelector:@selector(function1:)) { } if ([obj isKindOfClass:[NSArray class]] ) { } if ([obj conformsToProtocol:@protocol(myProtocol)]) { } if ([[obj class] isSubclassOfClass:[NSArray class]]) { } [obj someNonExistFunction]; 

看似很简单的语句,但是为了让语言实现这个能力,语言开发者要付出很多努力实现runtime环境。这里运行时环境处理了弱类型、函数存在检查工作。runtime会检测注册列表里是否存在对应的函数,类型是否正确,最后确定下来正确的函数地址,再进行保存寄存器状态,压栈,函数调用等等实际的操作。

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
id knife=[Knife grateKnife]; NSArray *monsterList=[NSArray array]; [monsterList makeObjectsPerformSelector:@selector(killMonster:) withObject:knife]; 

用c,c++完成这个功能还是比较非常麻烦的,但是动态语言处理却非常简单并且这些语句让objc语言更加intuitive。

在Objc中针对对象的函数调用将不再是普通的函数调用,[obj function1With:var1]; 这样的函数调用将被运行时环境转换成objc_msgSend(target,@selector(function1With:),var1);。Objc的runtime环境是开源的,所以我们可以拿出一下实现做简单介绍,可以看到objc_msgSend由汇编语言实现,我们甚至不必阅读代码,只需查看注释就可以了解,运行时环境在函数调用前做了比较全面的安全检查,已确保动态语言函数调用不会导致程序crash。对于希望深入学习的朋友可以自行下载Objc-runtime源代码来阅读,这里就不再深入讲解。

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">10</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">11</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">12</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">13</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">14</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">15</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">16</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">17</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">18</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">19</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">20</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">21</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">22</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">23</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">24</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">25</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">26</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">27</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">28</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">29</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">30</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">31</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">32</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">33</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">34</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">35</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">36</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">37</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">38</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">39</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">40</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">41</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">42</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">43</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">44</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">45</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">46</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">47</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">48</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">49</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">50</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">51</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">52</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">53</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">54</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">55</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">56</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">57</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">58</span>
/********************************************************************  * id objc_msgSend(id self,  * SEL op,  * ...)  *  * On entry: a1 is the message receiver,  * a2 is the selector  ********************************************************************/   ENTRY objc_msgSend # check whether receiver is nil  teq a1, #0  moveq a2, #0  bxeq lr  # save registers and load receiver's class for CacheLookup  stmfd sp!, {a4,v1-v3}  ldr v1, [a1, #ISA]  # receiver is non-nil: search the cache  CacheLookup a2, LMsgSendCacheMiss  # cache hit (imp in ip) - prep for forwarding, restore registers and call  teq v1, v1 /* set nonstret (eq) */  ldmfd sp!, {a4,v1-v3}  bx ip  # cache miss: go search the method lists LMsgSendCacheMiss:  ldmfd sp!, {a4,v1-v3}  b _objc_msgSend_uncached  LMsgSendExit:  END_ENTRY objc_msgSend    .text  .align 2 _objc_msgSend_uncached:  # Push stack frame  stmfd sp!, {a1-a4,r7,lr}  add r7, sp, #16  SAVE_VFP  # Load class and selector  ldr a1, [a1, #ISA] /* class = receiver->isa */  # MOVE a2, a2 /* selector already in a2 */  # Do the lookup  MI_CALL_EXTERNAL(__class_lookupMethodAndLoadCache)  MOVE ip, a1  # Prep for forwarding, Pop stack frame and call imp  teq v1, v1 /* set nonstret (eq) */  RESTORE_VFP  ldmfd sp!, {a1-a4,r7,lr}  bx ip 

现在说一下runtime的负面影响: 1. 关于执行效率问题。 “静态语言执行效率要比动态语言高”,这句没错。因为一部分cpu计算损耗在了runtime过程中,而从上面的汇编代码也可以看出,大概损耗在哪些地方。而静态语言生成的机器指令更简洁。正因为知道这个原因,所以开发语言的人付出很大一部分努力为了保持runtime小巧上。所以objecitve-c是c的超集+一个小巧的runtime环境。 但是,换句话说,从算法角度考虑,这点复杂度不算差别的,Big O notation结果不会有差别。( It’s not log(n) vs n2 ) 2. 另外一个就是安全性。动态语言由于运行时环境的需求,会保留一些源码级别的程序结构。这样就给破解带来的方便之门。一个现成的说明就是,java,大家都知道java运行在jre上面。这就是典型的runtime例子。它的执行文件.class全部可以反编译回近似源代码。所以这里的额外提示就是如果你需要写和安全有关的代码,离objc远点,直接用c去。

简单理解:“Runtime is everything between your each function call.”

但是大家要明白,第二点我提到runtime并不只是因为它带来了这些简便的语言特性。而是这些简单的语言特性,在实际运用中需要你从完全不同的角度考虑和解决问题。只是计算1+1,很多语言都是一样的,但是随着问题的复杂,项目的增长,静态语言和动态语言就会演化出完全不同的风景。

3. thread

“thread synchronization another notorious trouble!”

记得上学时候学操作系统这门课,里面都会有专门一章介绍任务调度和生产者消费者的问题。 这就是为今后使用进程、线程开发打基础。概念很简单,但难点在synchronization(同步),因为死锁检测算法不是100%有效,否则就根本没有死锁这个说法了。另一个原因是往往这类错误很隐晦,静态分析很难找到。同时多线程开发抽象度较高需要经验去把握。

总体来说,我见到的在这方面的问题可以分为一下几点:

1. 对系统整体结构认识模糊

不知道多线程开发的几个基点,看别人代码越看越糊涂的。一会NSThread,一会Grand Central Dispatch、block,一会又看到了pthread等等。Apple封装了很多线程的API, 多线程开发的基本结构入下图:

可以看到在多线程开发中你可以选择这上面这4种不同的方式。

Mach是核心的操作系统部分。其实这个我也不是非常熟悉,至少我还没有读到过直接使用mach做多线程的代码。

pthread(POSIX Threads)是传统的多线程标准,灵活、轻巧,但是需要理论基础,开发复杂。需要注意一点,根据apple文档提示,在Cocoa下使用pthread需要先启动至少一个NSThread,确定进入多线程环境后才可以。

NSThread是Mac OS 10.0后发布的多线程API较为高层,但是缺乏灵活性,而且和pthread相比效率低下。

Grand Central Dispatch 是10.6后引入的开源多线程库,它介于pthread和NSThread之间。比NSThread更灵活、小巧,并且不需要像pthread一样考虑很多lock的问题。同时objective-c 2.0发布的新语法特性之一blocks,也正是根据Grand Central Dispatch需求推出的。

所以在你写多线程代码或者阅读多线程代码时候,心理要先明确使用哪种技术。

2. thread和Reference Counting内存管理造成的问题。

线程里面的方法都要放到NSAutoreleasePool里面吗?

这类问题很常见,迷惑的原因是不明白 NSAutoreleasePool 到底是干什么用的。NSAutoreleasePool跟thread其实关系并不显著,它提供一个临时内存管理空间,好比一个沙箱,确保不会有不当的内存分配泄露出来,在这个空间内新分配的对象要向这个pool做一下注册告诉:“pool,我新分配一块空间了”。当pool drain掉或者release,它里面分配过的内存同样释放掉。可见和thread没有很大关系。但是,我们阅读代码的时候经常会看到,新开线程的函数内总是以NSAutoreleasePool开始结束。这又是为什么呢!? 因为thread内恰好是最适合需要它的地方! 线程函数应该计算量大,时间长(supposed to be heavy)。在线程里面可能会有大量对象生成,这时使用autoreleasepool管理更简洁。所以这里的答案是,不一定非要在线程里放NSAutoreleasePool,相对的在cocoa环境下任意地方都可以使用NSAutoreleasePool。如果你在线程内不使用NSAutoreleasePool,要记得在内部alloc和relase配对出现保证没有内存泄露。

3. 线程安全

每个程序都有一个主线程(main thread),它负责处理事件响应,和UI更新。

更新UI问题。很多新手会因为这个问题,导致程序崩溃或出现各种问题。而且逛论坛会看到所以人都会这么告诉你:“不要在后台线程更新你的UI”。其实这个说法不严密,在多线程环境里处理这个问题需要谨慎,而且要了解线程安全特性。

首先我们需要把“UI更新”这个词做一个说明,它可以有2个层次理解,首先是绘制,其次是显示。这里绘制是可以在任何线程里进行,但是要向屏幕显示出来就需要在主线程操作了。我举个例子说明一下,例如现在我们有一个NSImageView,里面设置了一个NSImage, 这时我想给NSImage加个变色滤镜,这个过程就可以理解为绘制。那么我完全可以再另外一个线程做这个比较费时的操作,滤镜增加完毕再通知NSImageView显示一下。另一个例子就是,Twitter客户端会把每一条微博显示成一个cell,但是速度很快,这就是因为它先对cell做了offscreen的渲染,然后再拿出来显示。

所以通过这点我们也可以得到进一步的认识,合理设计view的更新是非常重要的部分。很多新手写得代码片段没错,只是因为放错了地方就导致整个程序出现各种问题。

根据苹果线程安全摘要说明,再其它线程更新view需要使用lockFocusIfCanDraw和unlockFocus锁定,确保不会出现安全问题。

另外还要知道常用容器的线程安全情况。immutable的容器是线程安全的,而mutable容器则不是。例如NSArray和NSMutableArray。

4. Asynchronous(异步) vs. Synchronous(同步)

我在一个view要显示多张web图片,我想问一下,我是应该采用异步一个一个下载的方式,还是应该采用多线程同时下载的方式,还是2个都用,那种方式好呢?

实际上单独用这2个方法都不好。并不是简单的用了更多线程就提高速度。这个问题同时涉及客户端和服务器的情况。

处理这种类型的程序,比较好的结构应该是:非主线程建立一个队列(相当于Asynchronous任务),队列里同时启动n个下载任务(相当于Synchronous任务)。这里的n在2~8左右就够了。这个结构下可以认为队列里面每n个任务之间是异步关系,但是这n个任务之间又是同步关系,每次同时下载2~8张图片。这样处理基本可以满足速度要求和各类服务器限制。

5. thread和run­loop

runloop是线程里的一部分,但我觉得有必要单独拿出来写,是因为它涉及的东西比较容易误解,而说明它的文章又不多。

4. run­loop

thread和runloop在以前,开发者根本不太当成一个问题。因为没有静态语言里runloop就是固定的线程执行loop。而现在Cocoa新手搞不明白的太多了,因为没有从动态角度看它,首先回想一下第2点介绍的runtime概念,接着出一个思考题。

现在有一个程序片段如下:

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">10</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">11</span>
- (void)myThread:(id)sender {  NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];  while (TRUE) {  //do some jobs  //break in some condition  usleep(10000);  [pool drain];  }  [pool release]; } 

现在要求,做某些设计,使得当这个线程运行的同时,还可以从其它线程里往它里面随意增加去掉不同的计算任务。 这,就是NSRunloop的最原始的开发初衷。让一个线程的计算任务更加灵活。 这个功能在c, c++里也许可以做到但是非常难,最主要的是因为语言能力的限制,以前的程序员很少这么去思考。

好,现在我们对上面代码做一个非常简单的进化:

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">10</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">11</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">12</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">13</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">14</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">15</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">16</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">17</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">18</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">19</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">20</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">21</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">22</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">23</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">24</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">25</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">26</span>
NSMutableArray *targetQueue; NSMutableArray *actionQueue; - (void)myThread:(id)sender {  NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];  while (TRUE) {   //do some jobs  //break in some condition  int n=[targetQueue count];  assert(n==[actionQueue count]);  for(int i=0;i<n;i++){  id target=[targetQueue objectAtIndex:i];  SEL action=NSSelectorFromString([actionQueue objectAtIndex:i]);  if ([target respondsToSelector:action]) {  [target performSelector:action withObject:nil];  }  }   usleep(10000);   [pool drain];  }   [pool release]; } 

注意,这里没有做线程安全处理,记住Mutable container is not thread safe. 这个简单的扩展,让我们看到了如何利用runtime能力让线程灵活起来。当我们从另外线程向targetQueue和actionQueue同时加入对象和方法时候,这个线程函数就有了执行一个额外代码的能力。

有人会问,哪里有runloop? 那个是nsrunloop? 看不出来啊。

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
while (TRUE) {  //break in some condition } 

这个结构就叫线程的runloop, 它和NSRunloop这个类虽然名字很像,但完全不是一个东西。以前在使用静态语言开始时候,程序员没有什么迷惑,因为没有NSRunloop这个东西。 我接着来说,这个NSRunloop是如何来得。

第二段扩展代码里面确实没有NSRunloop这个玩意儿,我们接着做第3次改进。 这次我们的目的是把其中动态部分抽象出来。

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">10</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">11</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">12</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">13</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">14</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">15</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">16</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">17</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">18</span>
@interface MyNSTimer : NSObject {  id target;  SEL action;  float interval;  CFAbsoluteTime lasttime; } - (void)invoke; @end  @implementation MyNSTimer - (void)invoke; {  if ([target respondsToSelector:action]) {  [target performSelector:action withObject:nil];  } } @end 
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">10</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">11</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">12</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">13</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">14</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">15</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">16</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">17</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">18</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">19</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">20</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">21</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">22</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">23</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">24</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">25</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">26</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">27</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">28</span>
@interface MyNSRunloop : NSObject {  NSMutableArray *timerQueue; } - (void)addTimer:(MyNSTimer*)t; - (void)executeOnce; @end  @implementation MyNSRunloop - (void)addTimer:(MyNSTimer*)t; {  @synchronized(timerQueue){  [timerQueue addObject:t];  } } - (void)executeOnce; {  CFAbsoluteTime currentTime=CFAbsoluteTimeGetCurrent();  @synchronized(timerQueue){  for(MyNSTimer *t in timerQueue){  if(currentTime-t.lasttime>t.interval){  t.lasttime=currentTime;  [t invoke];  }  }  } } @end 
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">10</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">11</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">12</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">13</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">14</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">15</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">16</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">17</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">18</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">19</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">20</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">21</span>
@interface MyNSThread : NSObject {  MyNSRunloop *runloop; } - (void)main:(id)sender; @end  @implementation MyNSThread - (void)main:(id)sender {  NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];  while (TRUE) {  //do some jobs  //break in some condition  [runloop executeOnce];  usleep(10000);  [pool drain];  }  [pool release]; } @end 

走到这里,我们就算是基本把Runloop结构抽象出来了。例如我有一个MyNSThread实例,myThread1。我可以给这个实例的线程添加需要的任务,而myThread1内部的MyNSRunloop对象会管理好这些任务。

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
MyNSTimer *timer1=[MyNSTimer scheduledTimerWithTimeInterval:1  target:obj1  selector:@selector(download1:)]; [myThread1.runloop addTimer:timer1];  MyNSTimer *timer2=[MyNSTimer scheduledTimerWithTimeInterval:2  target:obj2  selector:@selector(download2:)]; [myThread1.runloop addTimer:timer2]; 

当你看懂了上面的代码也许会感叹,‘原来是这么回事啊!为什么把这么简单的功能搞这么复杂呢?’ 其实就是这么回事,把Runloop抽象出来可以使得线程任务管理更加loose coupling,给设计模式提供更大的空间。这样第三方开发者不需要过深入的涉及线程内部代码而轻松管理线程任务。另外请注意,这里MyNSRunloop, MyNSTimer等类是我写得一个模拟情况,真实的NSRunloop实现肯定不是这么简单。这里为了说明一个思想。这种思想贯穿整个cocoa framework,从界面更新到event管理。

5. delegate, protocol

这个会列出来因为,我感觉问它的数量仅此于内存管理部分,它们用得很频繁,并且这些是设计模式的重要组成部分。

待写…

6. event respon­der

使用过Xcode的开发者都知道Interface Builder这个开发组件,在Xcode4版本以后该组件已经和xcode整合到一起。它是苹果软件开发中非常重要的部分。ib为开发者减轻了很大一部分界面设计工作。但是其中有一个东西让新接触ib的开发者一头雾水,那就是First Responder, 它是什么东西,为何它会有那么多Actions。这节我会详细介绍如何理解Responder和Cocoa下的事件响应链。

First Responder在IB属性为Placeholders,这意味着它属于一个虚拟实例。就好比TextField里面的string placeholder一样,只是临时显示一下。真正的first responder会被其它对象代替。实际上,任何派生自NSResponder类的对象都可以替代First Responder。而First Responder里面的所有Actions就是NSResponder提供的接口函数,当然你也可以定义自己的响应函数。

MacOS在系统内部会维护一个称为“The Responder Chain”的链表。该列表内容为responder对象实例,它们会对各种系统事件做出响应。最上面的哪个对象就叫做first responder,它是最先接收到系统事件的对象。如果该对象不处理该事件,系统会将这个事件向下传递,直到找到响应事件的对象,我们可以理解为该事件被该这个对象截取了。

The Responder Chain基本结构如下图所示:

在理解了上面的概念之后,我希望使用一个例子让大家对responder有更加具体的认识。大家都知道NSTextField这个控件,它是最常见的控件之一。它最基本功能是显示一个字符串,如果启用可选,那么用户可以选中文本,拷贝文本,如果开启编辑选项,还可以运行用户编辑文本,等等基本操作。

下面展示给大家的例子是创建一个我们自己创建的简单textfield叫LXTextField。它不属于NSTextField而是派生自NSView,具有功能显示字符串,全选字符串,响应用户cmd+c的拷贝操作,三个基本功能。注意NSView派生自NSResponder。

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">10</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">11</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">12</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">13</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">14</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">15</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">16</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">17</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">18</span>
// // LXTextField.h // lxtextfield // // Created by xu lian on 12-03-09. // Copyright (c) 2012 Beyondcow. All rights reserved. //  #import <AppKit/AppKit.h>  @interface LXTextField : NSView {  NSString *stringValue;  BOOL selectAll; } @property(retain,nonatomic) NSString *stringValue;  @end 
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">9</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">10</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">11</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">12</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">13</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">14</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">15</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">16</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">17</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">18</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">19</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">20</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">21</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">22</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">23</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">24</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">25</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">26</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">27</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">28</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">29</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">30</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">31</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">32</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">33</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">34</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">35</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">36</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">37</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">38</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">39</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">40</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">41</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">42</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">43</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">44</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">45</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">46</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">47</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">48</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">49</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">50</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">51</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">52</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">53</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">54</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">55</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">56</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">57</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">58</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">59</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">60</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">61</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">62</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">63</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">64</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">65</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">66</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">67</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">68</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">69</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">70</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">71</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">72</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">73</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">74</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">75</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">76</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">77</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">78</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">79</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">80</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">81</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">82</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">83</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">84</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">85</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">86</span>
// // LXTextField.m // lxtextfield // // Created by xu lian on 12-03-09. // Copyright (c) 2012 Beyondcow. All rights reserved. //  #import "LXTextField.h"  @implementation LXTextField @synthesize stringValue;  - (void)awakeFromNib {  selectAll = NO; }  - (id)initWithFrame:(NSRect)frameRect {  if( self = [super initWithFrame:frameRect] ){  selectAll = NO;  }  return self; }  - (BOOL)acceptsFirstResponder {  return YES; }  - (BOOL)becomeFirstResponder {  return YES; }  - (BOOL)resignFirstResponder {  selectAll=NO;  [self setNeedsDisplay:YES];  return YES; }   - (void)setStringValue:(NSString *)string{  stringValue = string;  [self setNeedsDisplay:YES]; }  - (void)drawRect:(NSRect)dirtyRect {  if (selectAll) {  NSRect r = NSZeroRect;  r.size = [stringValue sizeWithAttributes:nil];  [[NSColor selectedControlColor] set];  NSRectFill(r);  }  [stringValue drawAtPoint:NSZeroPoint withAttributes:nil]; }  - (IBAction)selectAll:(id)sender; {  selectAll=YES;  [self setNeedsDisplay:YES]; }  - (IBAction)copy:(id)sender; {  NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];  [pasteBoard declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:nil];  [pasteBoard setString:stringValue forType:NSStringPboardType]; }  - (void)mouseDown:(NSEvent *)theEvent {  if ([theEvent clickCount]>=2) {  selectAll=YES;  }  [self setNeedsDisplay:YES]; }  - (void)keyDown:(NSEvent *)theEvent { }  @end 

运行实例,可以看到随着LXTextField收到系统发送的becomeFirstResponder消息,LXTextField变成responder chain中的frist responder, 这时候可以理解为IB里的哪个First Responder虚拟实例被该LXTextField取代。这时候mainMenu上哪些菜单项,例如:全选(cmd+a), 拷贝(cmd+a)等事件都会最先发给当前这个LXTextField。一旦你的LXTextField实现了NSResponder的哪些默认函数,那么该对象就会截取系统事件。当然这些事件具体如何实现还是需要你自己写代码实现。例如这里的 - (IBAction)copy:(id)sender; 显然我手动实现了textfield的copy能力。

注意上述代码中我实现了一个空函数- (void)keyDown:(NSEvent *)theEvent 这意味着我们希望LXTextField截取键盘事件而不再传递给responder chain后续对象。当然,如果我们希望LXTextField响应特定键盘事件,而其他事件继续传给其他响应对象,我们可以编写如下代码。

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">8</span>
- (void)keyDown:(NSEvent *)theEvent {  if(condition){  do something;  }else{  [super keyDown:theEvent];  } } 

待写…

7. mem­ory management

内存管理问题,也许是问得最多的问题了吧。

  1. 内存管理规则 Cocoa下程序开发内存管理使用ref­er­ence counting(RC)机制。从10.7以后apple开始推荐auto­matic ref­er­ence counting(ARC)机制。大家是否知道从旧时代的RC到ARC机制到底意味着什么呢?为什么ARC从开发速度,到执行速度和稳定性都要优于rc?

开发速度不言而喻,你少写很多release代码,甚至很少去操心这部分。

执行速度呢?这个还要从runtime说起,还记得我在第2点说得一句话么:“Runtime is everything between your each function call.”

RC有一个古老的内存管理哲学:谁分配谁释放。 通过counting来确定一个资源有几个使用者。道理很简单,但是往往简单的东西人却会犯错。从来没有一个程序员可以充满信心的说,我写得代码从来没有过内存泄露。这样来看,我们就更需要让程序可以自己处理这个管理机制,这就需要把这个机制放到runtime里。

所以RC->ARC就是把内存管理部分从普通开发者的函数中移到了函数外的runtime中。因为runtime的开发原型简单,逻辑层次更高,所以做这个开发和管理出错的概率更小。实际上编译器开发人员对这部分经过无数次测试,所以可以说用arc几乎不会出错。另外由于编译的额外优化,使得这个部分比程序员自己写得代码要快速很多。而且对于一些通用的开发模式,例如autorelease对象,arc有更优秀的算法保证autoreleasepool里的对象更少。

  1. RC规则 首先说一下rc是什么,r-Reference参照,引用 c-counting计数, rc就是引用计数。俗话说就是记录使用者的数量。 例如现在我有一个房间空着,大家可以进去随意使用,但是你进门前,需要给门口的计数牌子+1, 出门时候-1。 这时候这个门口的牌子就是该房间里的人数。一但这个牌子变为0我就可以把房间关闭。

这个规则可以让NSObject决定是不是要释放内存。当一个对象alloc时候,系统分配其一块内存并且object自动计数retainCount=1 这时候每当[object retain]一次retainCount+1(这里虽然简写也是rc不过是巧合或者当时开发人员故意选的retain这个词吧)每次[object release]时候retainCount-1 当retainCount==0时候object就真正把这快内存还给系统。

  1. 常用container的Reference Counting特性 这个规则很简单把。但是这块确实让新手最头疼的地方。问题出在,新手总想去验证rc规则,又总是发现和自己的期望不符合。 无数次看到有人写下如下句子
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
NSLog(@"%d",[object retainCount]);  while([object retainCount]>0){  [object release]; } 

当然了,我也做过类似的动作,那种希望一切尽在掌握中的心态。但是你会看到其他人告诉这么做完全没有意义,RC规则并不是这么用的。

首先,这个数字也许并不是你心目中的哪个。因为很难跟踪到底哪些地方引用的该资源。你建立的资源不光只有你的代码才会用到,你调用的各种Framework,Framework调用的Framework,都有可能改变这个资源的retainCount。

其次,这里每个数字意味着有其它对象引用该资源,这样的暴力释放很容易导致程序崩溃。就好比,其它人也许可以翻牌子把门口哪个牌子上的数字改变,但是这会出现问题。还有很多人在里面,把牌子变成0房间锁了结果谁也出不来。又或者,减少牌子上的数字,人进的过多房间变得过于拥挤。

所以去验证rc规则,或者单纯的改变retainCount并不是明智之举。你能做的就是理解规则,使用规则,读文档了解container的引用特性。或者干脆移到arc上面,让runtime环境处理这些问题。

我有一个NSMutableArray里面保存了1000个NSString对象,我在release的时候需要循环释放1000个string么?还是只需要release NSMutableArray。

就像上面提到的,如果你了解container的引用特性,这个问题自然就解决了。“NSMutableArray在添加、插入objects时会做retain操作。” 通过这一句话就分析出,用户不否需要帮助NSMutableArray释放1000个string。回忆上面提到的管理哲学,“谁分配谁释放” 编写NSMutableArray的程序员非常熟悉这个规则,NSMutableArray内部retain了,NSMutableArray自然要负责release。但是NSMutableArray才不会管你在外面什么地方引用了这1000个string,它只管理好内部的rc就够了。所以如果你在NSMutableArray外面对1000个string retain了,你自然需要release。相应的,你作为创建这个NSMutableArray的程序员,你只管release这个NSMutableArray就可以了。

最后说一下不用arc的情况。目前情况来看,有不少第三方的库并未支持arc,所以如果你的旧项目使用了这些库,请检查是否作者发布了新版本,或者你需要自己修正支持arc。

8. class heritage and category

在这节里说明2个比较重要的问题,第一是subclass和category的区别。第二是新手最容易忽略的学习步骤。

Objective-C的Object-oriented programming特性提供subclass和category这2个比较非常重要的部分。subclass应该反复被各种编程书籍介绍过。它是oop继承特性的关键语法,它给类添加了延续并且多样化自己的方法。可以说没有继承就没有oop这玩意。而category相对于subclass就不那么出名了。其实category思想出世于smalltalk,所以它不能算是一个新生事物。

先说一下这2个特性最主要的区别。简单可以这么理解,subclass体现了类的上下级关系,而category是类间的平级关系。

如上图所示,左侧是subclass,可以看到class, subclass1, subclass2是递进关系。同时下面的子类完全继承父类的方法,并且可以覆盖父类的方法。subclass2拥有function1,function2,function3三个函数方法。function1的执行代码来自subclass1, function2的执行代码来自于subclass2。

右侧是category。可以看到,无论如何扩展类的category,最终就只有一个类class。category可以说是类的不同方法的小集合,它把一个类的方法划分成不同的区块。请注意观察,每个category块内的方法名称都没有重复的。这正是category的重要要求。

经过上面简单解释了解了这2点的基本区别,现在深入说一下category。

在Objective-c语言设计之初一个主要的哲学观点就是尽量让一个程序员维护庞大的代码集。(对于庞大的项目‘原则’和‘协议’是非常重要的东西。甚至编写良好的文件名都是非常重要的开发技巧)根据结构化程序设计的经验出发,把一个大块代码划分成一些小块的代码更便于程序员管理。于是objc借用了smalltalk的categories概念。允许程序员把一系列功能相近的方法组织到一个单独的文件内,使得这些代码更容易识别。

更进一步的,和c,c++这种静态语言相比。objc把class categories功能集成到了run-time里面。因此,objc的categories允许程序员为已经存在的类添加新的方法而不需要重新编译旧的类。一旦一个category加入,它可以访问该类所有方法和实例变量,包括私有变量。

category不仅可以为原有class添加方法,而且如果category方法与类内某个方法具有同样的method signature,那么category里的方法将会替换类的原有方法。这是category的替换特性。利用这个特性,category还可以用来修复一些bugs。例如已经发布的Framework出现漏洞,如果不便于重新发布新版本,可以使用category替换特性修复漏洞。另外,由于category有run-time级别的集成度,所以使得cocoa程序安全性有所下降。许多黑客就是利用这个特性(和posting技术2)劫持函数、破解软件,或者为软件增加新功能。一个很典型的例子就是,我发布的QQ表情管理器。

值得注意的一点是,由于一个类的categories之间是平级关系。所以如果不同categories拥有相同的方法,这个调用结果是未知的。所以:

Category methods should not override existing methods (class or instance). Two different categories implementing the same method results in undefined behavior

Objc中Categories有其局限的部分,就是你不能为原有的class添加变量,只能添加方法。当然方法里可以添加局部变量。在这个局限基础上就有其它语言做了进一步改进,例如TOM语言就为Categories增加了添加类变量的能力。

总上所属,如果你开发时候遇到无论如何都需要为类添加变量的情况,最好的选择就是subclass。相反如果你只希望增加一些函数簇。Categories是最好的选择。

Categories关注的重心是代码设计,把不同功能的方法分离开。在Objc里因为Categories是runtimes级别的特性,所以这种分离不仅体现在源码结构上,同时体现在运行时过程中。这意味着一个category里的方法在程序运行中如果没有被调用,那么它就不会被加载到内存中。所以合理的使用categories会减少你的程序内存消耗。

所以我个人给大家的建议是,每个Cocoa程序员都应该收集整理自己的一套NS类函数的Categories扩展库。这对你今后程序开发效率和掌控情况都有很大提高。

待写…

9. design pattern

待写…

10. Drawing Issues

大家也许都知道,MacOS是一个非常注重UI的系统。所以在MacOS编程里绘制是一个非常重要的部分。第10部分,我会从2点介绍MacOS下绘制编程。首先是绘制技术分类;其次是绘制代码结构。

从绘制技术分类上看,Cocoa程序员能接触的几种技术如下:Cocoa Drawing, Core Image, Core Animation, Quazrtz 2D(Core Graphics), OpenGL 5种。在这里我不打算给大家介绍每一种都是如何绘制具体的图像。只是介绍一下,它们大概长什么样子,并且有什么优势和限制。

Cocoa Drawing

Cocoa Drawing应该是学习Cocoa程序开发最先接触的绘制技术。也是目前大多数MacOS程序所使用的绘制技术,其底层使用Quazrtz 2D(Core Graphics)。苹果对应文档为(https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html)。Cocoa Drawing并没有统一的绘制函数,所有绘制函数分散在几个主要的NS类的下面。例如, NSImage, NSBezierPath, NSString, NSAttributedString, NSColor, NSShadow,NSGradient …

所以很简单,当你看到如下代码就可以判断,使用的是Cocoa Drawing方法

<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">1</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">2</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">3</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">4</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">5</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">6</span>
<span class="line-number" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline; color: rgb(88, 110, 117) !important;">7</span>
[anImage drawInRect:rect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];  [@"some text" drawAtPoint:NSZeroPoint withAttributes:attrs];  NSBezierPath *p=[NSBezierPath bezierPathWithRect:rect]; [[NSColor redColor] set]; [p fill]; 

这种代码多出现在NSView的drawRect函数内。

最后说一下关于提问, 例如刚看到这个问题:

现在有A *a;A*b [NSMutableArray addObject : a]; [NSMutableArray replaceObjectAtIndex:0 withObject:b] 执行完这两个之后,拿可变数组里面的0的位置 就是b元素了,那这个时候a到哪里去了??是否还占用着内存,如果占用内存的话,又如何去释放??

It’s kind of silly. 我并不是想讽刺问问题的朋友。其实如果你真的了解了上面这些知识点,就不会再问这种问题的。 为什么不多思考一层呢,在问这个问题之前想想,到底为什么会问出这个问题? ”如果让你给NSMutableArray实现一个replaceObjectAtIndex函数你会怎么写?“ 难道连个[obj release]都考虑不到么?然后根据内存管理规则,它到底释放了没,不言自明了吧。

其实这种问题论坛里很多的。不妨在迷惑的时候,先问问自己为什么会迷惑。

1:这里其实很有意思,为何我用“更高层次思考”,而不是“更底层次”。作为一个编译器和语言开发人员,面对的问题确实更底层没错,但是他们思考的维度更高,更抽象,这样子。一个不算恰当的比方就好像一个三维世界的人处理二维世界的一条线的问题。

你可能感兴趣的:(10个迷惑新手的Cocoa&Objective-c开发问题)