本文主要是介绍IOS SDK详解之NSDictionary,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
前言:本文将要讲述的内容有
1.NSDictionary 以及 NSMutableDictionary 概述
2.常用属性方法举例(不常用的本文不会涉及)
一 NSDictionary/NSMutableDictionary概述
NSDictionary提供了一种key-value的数据存储方式。总的来说,任何对象都可以作为key,只要其遵循NSCopying协议。其中,key不能相同(由isEqual来判断)。key和value都不能为nil,如果要表达一个空的值,用NSNull。NSDictionary中的值不可变。
NSMutableDictionary是NSDictionary的子类,是可变的字典。
二 NSDictionary常用的属性方法举例
2.1 创建和初始化
创建兼初始化
<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;">(<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">instancetype</span>)<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">dictionaryWithContentsOfFile</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(NSString</span> *)<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">path</span> (<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">instancetype</span>)<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">dictionary</span>; (<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">instancetype</span>)<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">dictionaryWithDictionary</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(NSDictionary</span> *) (<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">instancetype</span>)<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">dictionaryWithObjects</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(NSArray</span> *)<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">objects</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">forKeys</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(NSArray</span> *)<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">keys</span> (<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">instancetype</span>)<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">dictionaryWithObjectsAndKeys</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:(id)firstObject</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>
初始化
<code class="hljs erlang 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-pp" style="box-sizing: border-box;">-<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSDictionary</span> *)</span>init; -<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSDictionary</span> *)</span>initWithContentsOfFile:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSString</span> *)</span>path; -<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSDictionary</span> *)</span>initWithDictionary:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSDictionary</span> *)</span>; -<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSDictionary</span> *)</span>initWithObjects:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSArray</span> *)</span>objects forKeys:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSArray</span> *)</span>keys; -<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSDictionary</span> *)</span>initWithObjectsAndKeys:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>firstObject;</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>
就个人而言,我比较习惯后一种。当然,快捷创建的方式不要忘记了
符号
@{}
举例:
<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-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * emptyDic = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> dictionary];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * firstDic = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"first"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1"</span>};<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * secondDic = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> alloc] initWithObjectsAndKeys:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value1"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value2"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</span>,<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</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>
2.2 count
返回key-value对的个数
<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-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;">"key1"</span>:@<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;">"key2"</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;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%d"</span>,dic<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.count</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.3 isEqualToDictionary比较两个dictionary内容是否一样。
<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-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * dic1 = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>:@<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;">"key2"</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;">NSDictionary</span> * dic2 = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</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;">"key1"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1"</span>};<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([dic1 isEqualToDictionary:dic2]) {<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;">"Equal contents"</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>
2.4 objectForKey: 和valueForKey 由属性获得内容
<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-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * dic1 = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>:@<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;">"key2"</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;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>,[dic1 objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</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>,[dic1 valueForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key2"</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>
2.5 allKeys 和 allValues 获得所有的key/value
<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-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * dic1 = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>:@<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;">"key2"</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> * keys = [dic1 allKeys];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> * values = [dic1 allValues];</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>
2.6 enumerateKeysAndObjectsUsingBlock 用Block的方式遍历
<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;">这里的stop决定了是否停止遍历。 <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * dic1 = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key1"</span>:@<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;">"key2"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2"</span>}; [dic1 enumerateKeysAndObjectsUsingBlock:^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> key, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj, <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;">"%@=>%@"</span>,[key description],[obj description]);}];</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></ul>
2.7 排序
keysSortedByValueUsingSelector:
keysSortedByValueUsingComparator :
keysSortedByValueWithOptions: usingComparator:
返回Keys的数组,顺序按照value排序顺序。
<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-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * numsDic = @{@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>):@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"second"</span>,@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>):@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"first"</span>,@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>):@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"thrid"</span>};<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * strDic = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id_1"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"first"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id_3"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"thrid"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id_2"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"second"</span>};<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> * numsSortedKeys = [numsDic keysSortedByValueUsingSelector:<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;">NSArray</span> * strSortedKyes = [strDic keysSortedByValueUsingComparator:^NSComparisonResult(<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-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * str1 = obj1;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * str2 = obj2;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> [str1 compare:str2];}];<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>,numsSortedKeys<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.description</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>,strSortedKyes<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.description</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></ul>
输出
2015-02-09 22:04:12.070 DictonaryExample[1037:23292] (
1,
2,
3
)
2015-02-09 22:04:12.070 DictonaryExample[1037:23292] (
“id_1”,
“id_2”,
“id_3”
)
2.8 过滤
keysOfEntriesPassingTest:
返回keys的集合,这些keys符合参数block的约束
<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-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * numsDic = @{@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>):@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"second"</span>,@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>):@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"first"</span>,@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>):@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"thrid"</span>};NSSet * filteredKyes = [numsDic keysOfEntriesPassingTest:^<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> key, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj, <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;">BOOL</span> result = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NO</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> * numKey = key;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (numKey<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.integerValue</span> > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) {result = <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;">return</span> result;}];<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>,filteredKyes<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.description</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></ul>
输出
2015-02-09 22:09:50.800 DictonaryExample[1099:25241] {(
3
)}
2.9写到文件
writeToFile:atomically
writeToURL:atomically
<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-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>) firstObject] ;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * fileName = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file"</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * filePath = [path stringByAppendingPathComponent:fileName];<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;">"key"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"value"</span>};<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> result = [dic writeToFile:filePath atomically:<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> (result) {<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;">"Success"</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></ul>
2.10Description-常常用来debug输出dictionary的内容。
在之前已经举例好多了,这里不再赘述
三 NSMutableDictionary的额外方法
3.1 添加元素
<code class="hljs erlang 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-pp" style="box-sizing: border-box;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>setObject:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>anObjectforKey:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id<<span class="hljs-variable" style="box-sizing: border-box;">NSCopying</span>>)</span>aKey - <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>setValue:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>valueforKey:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSString</span> *)</span>key - <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>setDictionary:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSDictionary</span> *)</span>otherDictionary</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>
注意,使用KVC的时候,key一定要是NSString。第三个函数是删除之前的元素,然后把otherDictionary元素放到当前dic中。
举例
<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-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableDictionary</span> * dic = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableDictionary</span> alloc] initWithObjectsAndKeys:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"object"</span>,@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key"</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>,dic<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.description</span>);[dic setDictionary:@{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"otherKey"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"otherValue"</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>,dic<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.description</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>
输出
2015-02-09 22:31:21.417 DictonaryExample[1232:31666] {
key = object;
}
2015-02-09 22:31:21.418 DictonaryExample[1232:31666] {
otherKey = otherValue;
}
3.2删除元素
<code class="hljs erlang 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-pp" style="box-sizing: border-box;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>removeObjectForKey:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>aKey - <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>removeAllObjects - <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>removeObjectsForKeys:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSArray</span> *)</span>keyArray</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>
比较容易忽视的是第三个,删除一组key。
举例
<code class="hljs lua 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;"> NSDictionary * dic = @{@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>):@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"first"</span>,@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>):@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"second"</span>,@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>):@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"thrid"</span>};NSMutableDictionary * mutableDic = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[NSMutableDictionary alloc] initWithDictionary:dic];[mutableDic removeObjectsForKeys:@[@(1),@(2)]]</span>;NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>,mutableDic.description);</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></ul>
输出
<code class="hljs fix 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-attribute" style="box-sizing: border-box;">2015-02-09 22:34:13.112 DictonaryExample[1273:32793] {3 </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> thrid; }</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></li></ul>
这篇关于IOS SDK详解之NSDictionary的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!