【IOS 开发学习总结-OC-29】★★objective-c之foundation 框架——(集合类)数组

时间:2022-01-21 17:02:56

NSArray的功能和用法

创建NSArray有2种方法:类方法和实例方法。区别是:类方法以 array 开始,实例方法以 init 开头。 
常见的创建NSArray的几种方法: 
1. array:创建一个不包含任何 元素的空NSArray。 
2. arrayWithObject:/initWithObject::创建只包含指定元素的 NSArray。 
3. arrayWithObjects:/initWithObjects::创建包含N个元素的 NSArray。 
4. arrayWithContentsOfFile:/initWithContentsOfFile::读取文件内容来创建NSArray。

NSArray集合最大的特点是集合元素有索引——它的大部分用法都与集合元素的索引有关。NSArray集合的方法大致可以分为如下几类: 
1. 查询集合元素在NSArray的索引 
2. 根据索引值取出NSArray集合中的元素 
3. 对集合元素整体调用方法 
4. 对NSArray集合进行排序 
5. 取出 NSArray集合中的部分元素组成新的集合

用法示范代码 : 
NSArrayTest.m

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc , <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[])
{
@autoreleasepool{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithObjects:
@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂iOS讲义"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂Android讲义"</span>
, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂Ajax讲义"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂XML讲义"</span>
, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Struts 2.x权威指南"</span> , <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"第一个元素:%@"</span> , [array objectAtIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//这里要说明一下 ,访问某索引处的元素,也可以用普通数组的下标表示法来访问,比如上面可以用下面这句代码替换</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NSLog(@"第一个元素:%@" , array[0]);</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"索引为1的元素:%@"</span> , [array objectAtIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]);
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"最后一个元素:%@"</span> , [array lastObject]);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取索引从2~5的元素组成的新集合</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* arr1 = [array objectsAtIndexes: [NSIndexSet
indexSetWithIndexesInRange:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)]];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , arr1);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取元素在集合中的位置</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂Android讲义的位置为:%ld"</span> ,
[array indexOfObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂Android讲义"</span>]);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取元素在集合的指定范围中的位置</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"在2~5范围疯狂Android讲义的位置为:%ld"</span> ,
[array indexOfObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂Android讲义"</span>
inRange:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)]); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// ①</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 向数组的最后追加一个元素。</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 原NSArray本身并没有改变,只是将新返回的NSArray赋给array</span>
array = [array arrayByAddingObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"孙悟空"</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 向array数组的最后追加另一个数组的所有元素。</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 原NSArray本身并没有改变,只是将新返回的NSArray赋给array</span>
array = [array arrayByAddingObjectsFromArray:
[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithObjects:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"宝玉"</span> , @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"黛玉"</span> , <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> ; i < array<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.count</span>; i++)
{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// NSLog(@"%@" , [array objectAtIndex:i]);</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 上面代码也可简写为如下代码</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , array[i]);

}
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取array数组中索引为5~8处的所有元素</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* arr2 = [array subarrayWithRange: NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 将NSArray集合的元素写入文件</span>
[arr2 writeToFile:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"myFile.txt"</span> atomically:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>];
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li></ul>

nil表示NSArray元素结束——这个 nil 元素不会存入NSArray集合中。 
上面的程序中用到了 NSIndexSet 集合,它与NSSet 集合功能相似,区别只是主要用于保存索引值 ,所以,它的集合元素都是 NSUInteger 对象。

上面的代码在 NSArray 集合后追加元素后,对于原来的NSArray对象都不会产生任何修改(NSArray集合时候hi 不可修改的),程序只是返回一个新的NSArray对象。

NSArray怎么判断集合是否包含指定元素呢?

判断指定元素位于NSArray 集合中的索引,那么NSArray怎么判断集合是否包含指定元素呢? 
判断标准只有一个:只有某个集合元素与被查找元素通过 isEqual:方法比较返回 YES,即可认为该NSArray集合包含该元素,并不需要2个元素是同一元素。

参考资料: 
关于NSArray的比较机制可以参考:《疯狂 IOS 讲义(上)》的P273-275

对集合元素整体调用方法

如何简单地对NSArray集合所有或部分元素整体调用方法

NSArray允许对集合中所有的元素或 部分元素整体调用方法,如果只是简单地调用集合元素的方法,可以通过 NSArray的如下2种方法: 
1. makeObjectsPerformSelector:——依次调用NSArray集合中 每个元素的指定方法,该方法需要传入一个 SEL 参数,用于指定调用哪种方法。 
2. makeObjectsPerformSelector:withObject:依次调用NSArray集合中 每个元素的指定方法,该方法第一个 SEL 参数,用于指定调用哪种方法。第二个参数用于调用集合元素得到方法时传入参数。

如何对NSArray集合中的所有元素进行隐式遍历,并使用集合元素来执行某一段代码

可以通过NSArray的如下方法来完成: 
1. enumerateObjectsAtIndexes: options: usingBlock:——遍历集合中指定范围内的元素,并依次使用元素来执行指定的代码块。该方法可以传入一个参数,用于控制遍历的选项,如反向遍历。 
2. enumerateObjectsUsingBlock:——遍历集合中所有的元素,并依次使用 元素来执行指定的代码块。 
3. enumerateObjectsWithOptions:usingBlock:遍历集合中所有的元素,并依次使用 元素来执行指定的代码块。该方法可以传入一个参数,用于控制遍历的选项,如反向遍历。

说明:上面3个方法中,都需要传入一个代码块参数,该代码块必须带3个参数——第一个参数代表正在遍历的集合元素,第二个参数代表正在遍历的集合元素的索引。

示例代码: 
FKUser.h

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span>

<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">FKUser</span> : <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">NSObject</span></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span> , copy) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>* name;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span> , copy) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>* pass;
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>) initWithName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*) aName
pass:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*) aPass;
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>) say:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*) content;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

FKUser.m

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"FKUser.h"</span></span>

<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@implementation</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">FKUser</span></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@synthesize</span> name;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@synthesize</span> pass;
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>) initWithName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*) aName
pass:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*) aPass
{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> init])
{
name = aName;
pass = aPass;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>;
}
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>) say:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*) content
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@说:%@"</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.name</span> , content);
}
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 会重写isEqual:方法,重写该方法的比较标准是,</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 如果两个FKUser的name、pass相等,即可认为两个FKUser相等。</span>
- (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>) isEqual:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>)other
{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> == other)
{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>([other class] == FKUser<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.class</span>)
{
FKUser* target = (FKUser*)other;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.name</span> isEqualToString:target<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.name</span>]
&& [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.pass</span> isEqualToString:target<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.pass</span>];
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NO</span>;
}
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 重写description方法,可以直接看到FKUser对象的状态</span>
- (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>*) description
{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:
@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<FKUser[name=%@, pass=%@]>"</span>
, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.name</span> , <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.pass</span>];
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li></ul>

FKUserTest.m

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"FKUser.h"</span></span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc , <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[])
{
@autoreleasepool{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化NSArray对象</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithObjects:
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sun"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"123"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"bai"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"345"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zhu"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"654"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"tang"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"178"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"niu"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"155"</span>],
<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 对集合元素整体调用方法</span>
[array makeObjectsPerformSelector:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@selector</span>(say:)
withObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下午好,NSArray真强大!"</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>* content = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂iOS讲义"</span>;
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 迭代集合内指定范围内元素,并使用该元素来执行代码块</span>



[array enumerateObjectsAtIndexes:
[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)]
options:NSEnumerationReverse
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 代码块的第一个参数代表正在遍历的集合元素</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 代码块的第二个参数代表正在遍历的集合元素的索引</span>
usingBlock: ^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj, NSUInteger idx, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> *stop)
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"正在处理第%ld个元素:%@"</span> , idx , obj);
[obj say:content];
}];
}
}<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"FKUser.h"</span></span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc , <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[])
{
@autoreleasepool{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化NSArray对象</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithObjects:
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sun"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"123"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"bai"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"345"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zhu"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"654"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"tang"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"178"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"niu"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"155"</span>],
<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 对集合元素整体调用方法</span>
[array makeObjectsPerformSelector:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@selector</span>(say:)
withObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下午好,NSArray真强大!"</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>* content = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂iOS讲义"</span>;
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 迭代集合内指定范围内元素,并使用该元素来执行代码块</span>



[array enumerateObjectsAtIndexes:
[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)]
options:NSEnumerationReverse
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 代码块的第一个参数代表正在遍历的集合元素</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 代码块的第二个参数代表正在遍历的集合元素的索引</span>
usingBlock: ^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj, NSUInteger idx, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> *stop)
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"正在处理第%ld个元素:%@"</span> , idx , obj);
[obj say:content];
}];
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li></ul>

编译运行结果:

<code class="hljs css has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 07<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:58</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:26</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.724</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1066:16059]</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">sun</span>说:下午好,<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">NSArray</span>真强大!
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 07<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:58</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:26</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.726</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1066:16059]</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">bai</span>说:下午好,<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">NSArray</span>真强大!
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 07<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:58</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:26</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.727</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1066:16059]</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zhu</span>说:下午好,<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">NSArray</span>真强大!
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 07<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:58</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:26</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.727</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1066:16059]</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">tang</span>说:下午好,<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">NSArray</span>真强大!
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 07<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:58</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:26</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.728</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1066:16059]</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">niu</span>说:下午好,<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">NSArray</span>真强大!
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 07<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:58</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:26</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.729</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1066:16059]</span> 正在处理第3个元素:<<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">FKUser</span><span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[name=tang, pass=178]</span>>
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 07<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:58</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:26</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.730</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1066:16059]</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">tang</span>说:疯狂<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">iOS</span>讲义
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 07<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:58</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:26</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.730</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1066:16059]</span> 正在处理第2个元素:<<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">FKUser</span><span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[name=zhu, pass=654]</span>>
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 07<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:58</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:26</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.730</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1066:16059]</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">zhu</span>说:疯狂<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">iOS</span>讲义</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

对 NSArray 进行排序

NSArray提供的对集合元素进行排序的方法,都以 sort 开头。常用的排序方法如下: 
1. sortedArrayUsingSelector:——使用集合元素自身的方法对集合元素进行排序,集合元素的方法必须返回 NSOrderedDescending,NSOrderedAscending,NSOrderedSame 这些枚举值,用于代表集合元素的大小。本方法返回一个排好序的新NSArray对象。 
2. sortedArrayUsingFunction:context:该方法使用排序函数对集合元素进行排序,该排序函数必须返回NSOrderedDescending,NSOrderedAscending,NSOrderedSame 这些枚举值,用于代表集合元素的大小。本方法返回一个排好序的新NSArray对象。 
3. sortedArrayUsingComparator:该方法使用代码块对集合元素进行排序,该代码块必须返回NSOrderedDescending,NSOrderedAscending,NSOrderedSame 这些枚举值,用于代表集合元素的大小。本方法返回一个排好序的新NSArray对象。

实际上,sortedArrayUsingComparator:sortedArrayUsingFunction:context:的简化版本。 
示例代码:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span>

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 定义比较函数,根据两个对象的intValue进行比较</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSInteger</span> intSort(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> num1, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> num2, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> *context)
{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> v1 = [num1 intValue];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> v2 = [num2 intValue];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (v1 < v2)
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NSOrderedAscending;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (v1 > v2)
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NSOrderedDescending;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NSOrderedSame;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc , <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[])
{
@autoreleasepool{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化一个元素为NSString的NSArray对象</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array1 = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithObjects:
@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Objective-C"</span> , @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"C"</span> , @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"C++"</span>
, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Ruby"</span> , @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Perl"</span> , @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Python"</span> , <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用集合元素的compare:方法执行排序</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NSString 自身已经实现了 compare:方法,NSString本身就可以比较大小——NSString比较大小的方法是根据字符对应的编码进行的。</span>
array1 = [array1 sortedArrayUsingSelector:
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@selector</span>(compare:)];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , array1);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化一个元素为NSNumber的NSArray对象</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array2 = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithObjects:
[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithInt:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>],
[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithInt:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>],
[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithInt:-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>],
[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithInt:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>],
[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithInt:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span>], <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用intSort函数执行排序 </span>
array2 = [array2 sortedArrayUsingFunction:intSort
context:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , array2);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用代码块对集合元素进行排序</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array3 = [array2 sortedArrayUsingComparator:
^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj1, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj2)
{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 该代码块就是根据集合元素的 intValue进行比较</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([obj1 intValue] > [obj2 intValue])
{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NSOrderedDescending;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([obj1 intValue] < [obj2 intValue])
{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NSOrderedAscending;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NSOrderedSame;
}];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , array3);
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li></ul>

编译运行结果:

<code class="hljs css has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 08<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:27</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:34</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.174</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1121:26309]</span> (
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">C</span>,
"<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">C</span>++",
"<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">Objective-C</span>",
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">Perl</span>,
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">Python</span>,
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">Ruby</span>
)
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 08<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:27</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:34</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.176</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1121:26309]</span> (
"<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-8</span>",
12,
19,
20,
50
)
2015<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-10-03</span> 08<span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:27</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:34</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.177</span> 923<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[1121:26309]</span> (
"<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-8</span>",
12,
19,
20,
50
)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul>

自然排序与定制排序 
自然排序:集合元素本身可比较大小,直接利用集合元素比较大小的方法进行排序的方式,称为自然排序。 
定制排序:通过比较函数或代码块来指定自定义比较规则的方式,称为定制排序。

使用枚举器遍历 NSArray 集合元素

除了根据数组集合元素的索引来遍历集合元素外,还可以调用NSArray对象的如下2个方法来返回枚举器。 
1. objectEnumerator:——返回数组的顺序枚举器。 
2. reverseObjectEnumerator:——返回数组的逆序枚举器。

上面的2个方法都返回一个 NSEnumerator 枚举器,该枚举器只包含如下2个方法: 
1. allObjects:获取被枚举集合中的所有元素。 
2. nextObject:获取被枚举集合中的下一个元素。

一般来说,借助nextObject方法即可对集合元素进行枚举——程序通过不断获取 nextObject 方法的返回值,直到该方法的返回值为 nil 结束循环。

示例程序:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc , <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[])
{
@autoreleasepool{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 读取前面写入磁盘的文件,用文件内容来初始化NSArray集合</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithContentsOfFile:
@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"myFile.txt"</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取NSArray的顺序枚举器</span>
NSEnumerator* en = [array objectEnumerator];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> object;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(object = [en nextObject])
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , object);
}
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"------下面逆序遍历------"</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取NSArray的逆序枚举器 </span>
en = [array reverseObjectEnumerator];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(object = [en nextObject])
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , object);
}
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>

输出结果;

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">孙悟空</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">宝玉</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">黛玉</span>
<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">下面逆序遍历</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">黛玉</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">宝玉</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">孙悟空</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

快速枚举(for …in)

快速枚举(for …in)可以适用(NSArray,NSSet,NSDictionary 等集合)。 
快速枚举的语法格式如下:

<code class="hljs fsharp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">varName</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">in</span>  <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">collection</span>)</span>
{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//varName 自动迭代访问每个元素</span>
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

其中, type是集合元素的类型,varName是一个形参名,快速枚举自动将集合元素依次赋给该变量。

如果使用快速枚举来遍历 NSDictionary 对象,快速枚举中循环计数器依次代表NSDictionary的每个 key 的值

示例代码:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc , <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[])
{
@autoreleasepool{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 读取前面写入磁盘的文件,用文件内容来初始化NSArray集合</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithContentsOfFile:
@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"myFile.txt"</span>];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> object in array)
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , object);
}
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>

快速枚举遍历数组集合元素无需获得数组长度,也无须根据索引来访问数组元素。快速枚举的实质是一个 foreach 循环,foreach 循环自动迭代数组的每个元素,每个元素被迭代一次后,foreach 循环自动结束。

可变数组(NSMutableArray)

NSArray是不可变数组,创建成功即不可更改。

NSArray只是保存对象的指针,因此NSArray只保证这些指针变量中的地址不能改变,但指针变量指向的变量是可以改变的。

NSMutableArray是NSArray的子类。代表可变的数组集合。 
NSMutableArray新增了如下不同开头的方法: 
1. 以 add 开头:——增加集合元素的方法 
2. 以 remove 开头:——删除集合元素的方法 
3. 以 replace 开头:——替换集合元素的方法 
4. 以 sort 开头:——对集合本身排序的方法

NSMutableArray同样提供了前面提到的3中排序方法。与 NSArray 的区别是:NSArray的排序方法是返回一个新的,排好序的NSArray对象,而NSMutableArray的排序方法是集合本身排序。

示例代码:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span>

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 定义一个函数,该函数用于把NSArray集合转换为字符串</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>* NSCollectionToString(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array)
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableString</span>* result = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableString</span>
stringWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"["</span>];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj in array)
{
[result appendString:[obj description]];
[result appendString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", "</span>];
}
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取字符串长度</span>
NSUInteger len = [result length];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 去掉字符串最后的两个字符</span>
[result deleteCharactersInRange:NSMakeRange(len - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)];
[result appendString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"]"</span>];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> result;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc , <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[])
{
@autoreleasepool{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 读取前面写入磁盘的文件,用文件内容来初始化NSMutableArray集合</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span>* array = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span>
arrayWithContentsOfFile:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"myFile.txt"</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 向集合最后添加一个元素 </span>
[array addObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂iOS讲义"</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"最后追加一个元素后:%@"</span> , NSCollectionToString(array));
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用NSArray向集合尾部添加多个元素</span>
[array addObjectsFromArray: [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>
arrayWithObjects:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"张飞"</span> , @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"关羽"</span>,<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"最后追加两个元素后:%@"</span> , NSCollectionToString(array));
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 向集合的指定位置插入一个元素</span>
[array insertObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂Ajax讲义"</span> atIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"在索引为2处插入一个元素后:%@"</span>
, NSCollectionToString(array));
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用NSArray向集合指定位置插入多个元素</span>
[array insertObjects: [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>
arrayWithObjects:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"武松"</span> , @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"林冲"</span>,<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]
atIndexes:[NSIndexSet indexSetWithIndexesInRange
:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)]];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"插入多个元素后:%@"</span> , NSCollectionToString(array));
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 删除集合最后一个元素</span>
[array removeLastObject];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除最后一个元素后:%@"</span> , NSCollectionToString(array));
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 删除集合中指定索引处的元素</span>
[array removeObjectAtIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除索引为5处的元素后:%@"</span> , NSCollectionToString(array));
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 删除2~5处元素</span>
[array removeObjectsInRange:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"删除索引为2~5处的元素后:%@"</span>
, NSCollectionToString(array));
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 替换索引为2处的元素</span>
[array replaceObjectAtIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> withObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"疯狂Android讲义"</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"替换索引为2处的元素后:%@"</span>
, NSCollectionToString(array));
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li></ul>

NSArray的 kvc 与 kvo

NSArray允许直接对集合中的所有元素进行整体的 KVC 编码。NSArray提供了 下面2个方法(用途见示例代码) : 
1. valueForKey:——拿key 对应属性或实例变量,设置为value。 
2. setValue: forKey:

示例代码:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"FKUser.h"</span></span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 定义一个函数,该函数用于把NSArray集合转换为字符串</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>* NSCollectionToString(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array)
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableString</span>* result = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableString</span>
stringWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"["</span>];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj in array)
{
[result appendString:[obj description]];
[result appendString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", "</span>];
}
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取字符串长度</span>
NSUInteger len = [result length];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 去掉字符串最后的两个字符</span>
[result deleteCharactersInRange:NSMakeRange(len - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)];
[result appendString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"]"</span>];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> result;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc , <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[])
{
@autoreleasepool{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化NSArray对象</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span>* array = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithObjects:
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sun"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"123"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"bai"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"345"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"zhu"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"654"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"tang"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"178"</span>],
[[FKUser alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"niu"</span> pass:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"155"</span>],
<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取所有集合元素的name属性组成的新集合</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> newArr = [array valueForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , NSCollectionToString(newArr));
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 对集合中所有元素整体进行KVC编程</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 将所有集合元素的name属性改为"新名字"</span>
[array setValue:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"新名字"</span> forKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span> , NSCollectionToString(array));
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li></ul>

kvo

NSArray还为集合中的所有 元素或者部分元素 进行 KVO 编程提供了如下主要方法(其他方法可以自己尝试 ): 
1. addObserver: forKeyPath: options: context:为集合中是所有元素 添加 KVO 监听器。 
2. addObserver: toObjectsAtIndexes: forKeyPath: options: context: :为集合中指定索引处的 元素添加KVO 监听器。 
3. removeObserver: forKeyPath: 为集合中是所有元素删除 KVO 监听器 
4. removeObserver: fromObjectsAtIndexes: forKeyPath: :为集合中指定索引处的 元素删除KVO 监听器。