字典
OC中字典是一个类似与数组的容器
保存的是一个个 键值对
所谓键值对 就是 一个key(键)对应一个value(值) 两者合成一个键值对
字典保存的内容是无序的, 不能像数组一样通过下标取出元素
字典存取元素需要通过唯一标识的key, 通过key获得对应的值.
存元素的的时候也需要给定一个唯一标识的key
- 元素和值 都必须是对象
- key作为键值, 具有唯一性, 在开发中一般使用字符串作为key值
value保存的是对象, 可以是一个字符串对象, 可以是一个类对象, 也可以是一个数组, 还可以是一个字典.
这样就可以完成一个非常复杂的数据结构.
比如字典的value保存的是数组, 数组里的对象是字典, 字典里再保存数组. 这样不断嵌套. 理论上只要内存空间足够大, 可以有无数种嵌套形式.
1.不可变字典NSDictionary
- 特点:
- 字典⼀旦创建,键值对就不可更改,不可添加,不可删除。
- 仅能读取key或者value。
1. 常用方法
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>)objectForKey:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>)aKey; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 通过key获取key对应的对象</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;">readonly</span>, copy) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *allKeys; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获得所有key 通常与forin循环连用</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;">readonly</span>, copy) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *allValues; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获得所有values </span> </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li></ul>
2.便利构造器方法
<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">+ (instancetype)dictionary; // 无参数的初始化方法 + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> NS_REQUIRES_NIL_TERMINATION; // 带不定个数参数的初始化方法 // 注: 本方法的弊端是必须先添加一个value再添加一个key, 每添加两个对象是一个键值对 常常会导致记不清当前哪个是value哪个是key // 本方法常用 + (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys; </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">8</span></li></ul>
从网络和本地获取数据方法, 保存结果在字典中
<code class="hljs markdown has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-bullet" style="box-sizing: border-box;">+ </span>(NSDictionary <span class="hljs-emphasis" style="box-sizing: border-box;">*)dictionaryWithContentsOfFile:(NSString *</span>)path; <span class="hljs-bullet" style="box-sizing: border-box;">+ </span>(NSDictionary <span class="hljs-emphasis" style="box-sizing: border-box;">*)dictionaryWithContentsOfURL:(NSURL *</span>)url;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
利用两个数组给字典初始化
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *keys = @[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sex"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *values = @[@<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-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"18岁"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dic = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> dictionaryWithObjects:values forKeys:keys]; <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>, dic); </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">7</span></li></ul>
字典初始化 语法糖方法
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dic = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:@<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;">"age"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"18岁"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sex"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"男"</span>}; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *keys = [dic allKeys]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *values = [dic allValues]; <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;">"%@\n%@"</span>, keys, values); </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li></ul>
- 比较容易混淆的是 用语法糖时是先写key, 再写value. 而dictionaryWithObjectsAndKeys初始化方法是先写value再写key
接以上代码:
用objectForKey方法取值.
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">NSString <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*value</span> = [dic objectForKey:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@"</span>name”]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 因为键 name 对应的值是 字符串对象. 所以用一个字符串对象接收</span> NSLog(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@"</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%@</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", value);</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">也可以写成 NSString <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*value</span> = dic[<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@"</span>name”]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 类似于数组利用下标取值的方法. 写法上较为方便, 比较常用</span> NSLog(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@"</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%@</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", value);</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
值得注意的是, 字典的count保存的是键值对的对数. 比如以上代码中
[dic count]
的值为3 而不是6
可变字典 NSMutableDictionary
常用方法
- 删除键值对的方法
- (void)removeObjectForKey:(id)aKey;
- 删除所有键值对的方法
- (void)removeAllObjects;
- 添加键值对的方法
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
注意, 若是新添加的键值对, key是已添加过的key, 则新添加的键值对会覆盖之前key相同的键值对
常用的初始化方法
+ (instancetype)dictionary;
和
+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
用法为:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableDictionary</span> *dic1 = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableDictionary</span> dictionary]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableDictionary</span> *dic2 = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableDictionary</span> dictionaryWithCapacity:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 容量初始化大小随意, 因为可变数组超出后会自动扩充容量, 多出会自动回收</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
快速遍历(快速枚举): forin 语句
语句定义如下:
<code class="hljs delphi has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><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;">type</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> <#collection#>) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">{ <#statements#> }</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
代码解释:
- type *object 对象名. 类型为容器中存放的对象类型.
- collection 为需要遍历的容器: (数组或字典等)
- <#type *object#> in <#collection#>表示判断对象是否在需要遍历的容器中, 若是则自动便遍历, 否则不执行大括号里的语句
- <#type *object#> 在花括号中的语句执行结束后会重新定义并赋值, 寻找下一个容器里的同类型对象 直到所有同类型对象都被遍历过一次
- <#statements#>表示判断成功后执行的语句
值得注意的一点是, <#type *object#> 定义的对象的值在执行语句中可以被赋值于其他对象, 可以被输出, 但是不能被改变
另外, 用forin语句遍历字典的时候, 打印的是字典的key如:
<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">NSMutableDictionary *dic = [NSMutableDictionary dictionary]; // <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> // <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> // 给字典赋值语句省略. // <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> // <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (NSString *str <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> dic) { NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, str); }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">9</span></li></ul>
打印出来的结果是字典dic的key
集合
特点: 无序不重复
应用场景: 比如用来储存手机屏幕的像素点.
不可变集合NSSet
常用方法
- (id)anyObject;
返回一个计算机认为最合理的元素
常用的初始化方法:
<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">+ (instancetype)set; + (instancetype)setWithObjects:(id)firstObj, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> NS_REQUIRES_NIL_TERMINATION; + (instancetype)setWithArray:(NSArray *)array;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
初始化集合代码如下:
<code class="hljs sql has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">NSSet *<span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span> = [[NSSet alloc] initWithObjects:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"3"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>, nil];</span> NSLog(@"%@", <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span>);</span> NSLog(@"%lu", [<span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span> <span class="hljs-aggregate" style="box-sizing: border-box;">count</span>]);</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
由于集合是无序且不重复的,所以这里的count等于3 且打印set结果只有@“1”, @“2”, @“3”3个元素, 多余的@“2”不打印
获取集合中的一个元素
<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">NSString *<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = [set anObject]; 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;">str</span>);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
结果发现取出的永远是3, 这是计算机通过计算以后 返回的一个最合理的元素.
接集合的应用场景继续解释, 比如此时点击屏幕, 计算机会自动计算出最合理的一个元素, 既是我们点击的那个像素点, 返回.
可变集合NSMutableSet
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)addObject:(id)<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">object</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 添加对象</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)removeObject:(id)<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">object</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 删除对象</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li></ul>
NSCountedSet
- 初始化方法
- (instancetype)initWithSet:(NSSet *)set;
注意, 若是从NSSet 或者 NSMutableSet作为数据源的时候, 无论NSSet 或NSMutableSet中是否有重复元素.最后保存在NSCountedSet对象中都不存在重复元素.
- (instancetype)initWithArray:(NSArray *)array;
把数组当作数据源的时候, 是会有重复元素的.
- 常用方法
- (NSUInteger)countForObject:(id)object;
计算某一元素重复出现的次数
数组排序
不可变数组的排序
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">- (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *)sortedArrayUsingSelector:(SEL)comparator; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *array = @[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"3"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"5"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"4"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> * array2 = [array 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>, array2);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li></ul>
缺陷是一次只比字符串的一个字符, 当比较的是保存不同位的数字的字符串时, 结果往往是错的, 且结果是升序的. 这里的排序结果是 1 2 3 4 5
可变数组的排序方法:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)sortUsingSelector:(SEL)comparator; <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> arrayWithObjects:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"7"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"15"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"9"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]; [array sortUsingSelector:<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>, array); </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li></ul>
结果和不可变数组的排序缺陷相同. 这里的排序结果是:
1 15 2 7 9