版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/zsk_zane
结合之前的学习笔记以及参考《Objective-C编程全解(第三版)》,对Objective-C知识点进行梳理总结。知识点一直在变,只是作为参考,以苹果官方文档为准~
十二、快速枚举,枚举器和集合类NSSet
注意:下面的所提及的方法都是常用方法,全部列出不大现实,参考Xcode帮助文档即可~
1、遍历、快速遍历
(1)普通遍历:
<code class="hljs matlab 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> (NSUInteger <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">i</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">i</span> < <span class="hljs-transposed_variable" style="box-sizing: border-box;">array.</span>count; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">i</span>++) <span class="hljs-cell" style="box-sizing: border-box;">{ NSLog(@"%@", array[i]); }</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></ul>
(2)快速遍历
<code class="hljs ocaml 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> (id obj <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">array</span>) { NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, obj); }</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></ul>
快速遍历(快速枚举)语法
<code class="hljs bash 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-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</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></ul>
集合可以是可变集合也可以是不可变,但是可变集合在循环过程中不允许改变,否则会抛出异常
2、枚举器NSEnumerator
(1)基础知识:用来遍历集合类重元素对象的抽象类
(2)常用方法
<code class="hljs haml 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="ruby" style="box-sizing: border-box;"> (id)nextObject; <span class="hljs-regexp" 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;">nil</span>,通常与<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>结合使用遍历所有项 </span>-<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSArray</span> *)allObjects; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>返回集合中未被遍历的所有元素 </span>-<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSEnumerator</span> *)objectEnumerator; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>顺序遍历的枚举其 </span>-<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSEnumerator</span> *)reverseObjectEnumerator; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>逆序遍历的枚举器</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>
array4还是前一篇章十一中的定义,由于前一篇篇幅过长,因此分来两部分。
<code class="hljs scala has-numbering" style="color: inherit; display: block; padding: 0px; background-color: transparent; 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;"> NSEnumerator *enumerator = [array4 reverseObjectEnumerator]; id <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">object</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> ((<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">object</span> = [<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">enumerator</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">nextObject</span>]) != <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">nil</span>) {</span> NSLog(@<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;">object</span>); }</code><code class="hljs scala has-numbering" style="display: block; padding: 0px; background-color: transparent; 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;"><span style="color:#ff0000;">注意:</span></code><code class="hljs scala has-numbering" style="color: inherit; display: block; padding: 0px; background-color: transparent; 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;">在另一篇文章里看到</code><code class="hljs scala has-numbering" style="color: inherit; display: block; padding: 0px; background-color: transparent; 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;">for (<span class="hljs-keyword" style="font-family: 'Source Code Pro', monospace; font-size: 14px; white-space: pre; color: rgb(0, 0, 136); box-sizing: border-box;">object</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> = [</span><span class="hljs-title" style="font-family: 'Source Code Pro', monospace; font-size: 14px; white-space: pre; box-sizing: border-box; color: rgb(102, 0, 102);">enumerator</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-title" style="font-family: 'Source Code Pro', monospace; font-size: 14px; white-space: pre; box-sizing: border-box; color: rgb(102, 0, 102);">nextObject</span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">]</span>)</code><code class="hljs scala has-numbering" style="color: inherit; display: block; padding: 0px; background-color: transparent; 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;">{</code><code class="hljs scala has-numbering" style="color: inherit; display: block; padding: 0px; background-color: transparent; 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;"><span style="white-space:pre"> </span>...</code><code class="hljs scala has-numbering" style="color: inherit; display: block; padding: 0px; background-color: transparent; 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;">}</code><code class="hljs scala has-numbering" style="color: inherit; display: block; padding: 0px; background-color: transparent; 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;">这样写是错误的!!</code><code class="hljs scala has-numbering" style="color: inherit; display: block; padding: 0px; background-color: transparent; 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;"> </code><ul class="pre-numbering" style="color: rgb(51, 51, 51); 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></ul>
output:
<code class="hljs xml 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-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">MusicPlayer:</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">0x100406710</span>></span> obj2 1</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></ul>
(3)与for…in语法结合
<code class="hljs cs 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;"> NSEnumerator *enumerator = [array4 reverseObjectEnumerator]; id <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">object</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;">object</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> enumerator) { NSLog(<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;">object</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></ul>
output:
<code class="hljs xml 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-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">MusicPlayer:</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">0x100106870</span>></span> obj2 1</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></ul>
3、NSSet(单值对象的集合)
(1)NSSet与NSArray的异同
共同点:都是集合,能存放多个OC对象,且只能存放OC对象
不同点:NSArray有序排列,NSSet元素无序列排列;NSArray可容纳重复的对象,NSset不能重复,重复时前者会被替换。
(2)
(3)常用方法
初始化
<code class="hljs r 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;"> + (instancetype)set; + (instancetype)setWithObject:(id)object; - (instancetype)initWithObjects:(id)firstObj, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> NS_REQUIRES_NIL_TERMINATION; - (instancetype)initWithArray:(NSArray *)array; //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></ul>
其他
<code class="hljs haml 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="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSUInteger</span>)count; </span> -<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSArray</span> *)allObjects; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>将集合对象中所有元素以数组形式返回 </span> -<span class="ruby" style="box-sizing: border-box;"> (void)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">addObject:</span>(id)object; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>从集合中随机抽取一个元素 </span> -<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">BOOL</span>)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">containsObject:</span>(id)anObject; </span> -<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">BOOL</span>)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">intersectsSet:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSSet</span> *)otherSet; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>是否有共同的元素 </span> -<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">BOOL</span>)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">isEqualToSet:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSSet</span> *)otherSet; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>两个集合是否相等 </span> -<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">BOOL</span>)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">isSubsetOfSet:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSSet</span> *)otherSet; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>当前集合是否全部位于集合otherSet中</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>
(4)NSMutableSet常用方法
<code class="hljs haml 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="ruby" style="box-sizing: border-box;"> (void)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">addObject:</span>(id)object; </span> -<span class="ruby" style="box-sizing: border-box;"> (void)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">removeObject:</span>(id)object; </span> -<span class="ruby" style="box-sizing: border-box;"> (instancetype)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">initWithCapacity:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSUInteger</span>)numItems <span class="hljs-constant" style="box-sizing: border-box;">NS_DESIGNATED_INITIALIZER</span>; </span> -<span class="ruby" style="box-sizing: border-box;"> (void)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">unionSet:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSSet</span> *)otherSet; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>合并另个集合 </span> -<span class="ruby" style="box-sizing: border-box;"> (void)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">intersectSet:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSSet</span> *)otherSet; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>生成另个集合的交集 </span> -<span class="ruby" style="box-sizing: border-box;"> (void)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">minusSet:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSSet</span> *)otherSet; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>从当前集合中删除同otherSet共同的元素</span></code>