IOS SDK详解之NSDictionary

2024-02-28 23:38
文章标签 sdk 详解 ios nsdictionary

本文主要是介绍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, 


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] {( 

)}


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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/756966

相关文章

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

K8S(Kubernetes)开源的容器编排平台安装步骤详解

K8S(Kubernetes)是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。以下是K8S容器编排平台的安装步骤、使用方式及特点的概述: 安装步骤: 安装Docker:K8S需要基于Docker来运行容器化应用程序。首先要在所有节点上安装Docker引擎。 安装Kubernetes Master:在集群中选择一台主机作为Master节点,安装K8S的控制平面组件,如AP

嵌入式Openharmony系统构建与启动详解

大家好,今天主要给大家分享一下,如何构建Openharmony子系统以及系统的启动过程分解。 第一:OpenHarmony系统构建      首先熟悉一下,构建系统是一种自动化处理工具的集合,通过将源代码文件进行一系列处理,最终生成和用户可以使用的目标文件。这里的目标文件包括静态链接库文件、动态链接库文件、可执行文件、脚本文件、配置文件等。      我们在编写hellowor

LabVIEW FIFO详解

在LabVIEW的FPGA开发中,FIFO(先入先出队列)是常用的数据传输机制。通过配置FIFO的属性,工程师可以在FPGA和主机之间,或不同FPGA VIs之间进行高效的数据传输。根据具体需求,FIFO有多种类型与实现方式,包括目标范围内FIFO(Target-Scoped)、DMA FIFO以及点对点流(Peer-to-Peer)。 FIFO类型 **目标范围FIFO(Target-Sc

019、JOptionPane类的常用静态方法详解

目录 JOptionPane类的常用静态方法详解 1. showInputDialog()方法 1.1基本用法 1.2带有默认值的输入框 1.3带有选项的输入对话框 1.4自定义图标的输入对话框 2. showConfirmDialog()方法 2.1基本用法 2.2自定义按钮和图标 2.3带有自定义组件的确认对话框 3. showMessageDialog()方法 3.1

脏页的标记方式详解

脏页的标记方式 一、引言 在数据库系统中,脏页是指那些被修改过但还未写入磁盘的数据页。为了有效地管理这些脏页并确保数据的一致性,数据库需要对脏页进行标记。了解脏页的标记方式对于理解数据库的内部工作机制和优化性能至关重要。 二、脏页产生的过程 当数据库中的数据被修改时,这些修改首先会在内存中的缓冲池(Buffer Pool)中进行。例如,执行一条 UPDATE 语句修改了某一行数据,对应的缓

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序