-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
66 lines (38 loc) · 26.6 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Sven's Blog</title>
<subtitle>Long long journey.</subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://yoursite.com/"/>
<updated>2018-08-01T09:54:49.046Z</updated>
<id>http://yoursite.com/</id>
<author>
<name>Sven</name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title>[Objective-C] KVC 键值编码</title>
<link href="http://yoursite.com/2018/08/01/Objective-C-KVC-%E9%94%AE%E5%80%BC%E7%BC%96%E7%A0%81/"/>
<id>http://yoursite.com/2018/08/01/Objective-C-KVC-键值编码/</id>
<published>2018-08-01T08:54:05.000Z</published>
<updated>2018-08-01T09:54:49.046Z</updated>
<content type="html"><![CDATA[<h1 id="KVC"><a href="#KVC" class="headerlink" title="KVC"></a>KVC</h1><p><em>Key-Value Coding(简称:KVC)</em>是苹果提供的一种可以通过<code>key</code>来直接访问对象属性的机制。<br><img src="https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/Art/key_value_coding_2x.png" alt="Key Path"><br>而 Key Path 则是通过点语法来访问一个系列的对象属性。如图,访问的是:<code>employee1</code>对象内<code>manager</code>属性的<code>directReports</code>属性。</p><p><strong>实现原理:</strong><br><code>-valueForKey:</code>调用<code>property</code>的<code>get</code>方法,如果没有<code>get</code>方法(如:实例变量),则会直接返回实例变量的值。<br><code>-setValue:forKey:</code>调用<code>property</code>的<code>set</code>方法,如果没有<code>set</code>方法,则会直接给实例变量复制。 </p><h1 id="KVC-集合操作符"><a href="#KVC-集合操作符" class="headerlink" title="KVC 集合操作符"></a>KVC 集合操作符</h1><p><em>集合操作符(Collection operator)</em>是以<code>@</code>作为前缀的关键词,该关键词作用在<code>-valueForKeyPath:</code>语句中,其原理是在<code>getter</code>方法返回数据前进行对应的集合操作。</p><p>因为<a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/CollectionOperators.html#//apple_ref/doc/uid/20002176-SW9" target="_blank" rel="noopener">官方文档</a>中的定义比较晦涩,所以这里就根据返回值将集合操作符分为三个类型:</p><ul><li>简单集合操作符:返回一个反映数组集合在具体属性上操作后的单一值,如某个数值的平均值</li><li>对象操作符:返回一个<code>NSArray</code>数组对象</li><li>数组和集合运算符:返回一个数组或者集合,通常用于<code>联合</code>、<code>去重</code>操作</li></ul><p><strong>简单集合操作符:</strong></p><ul><li>@count: 返回一个值为集合中对象总数的<code>NSNumber</code>对象。</li><li>@sum: 首先把集合中的每个对象都转换为<code>double</code>类型,然后计算其总和,最后返回一个值为这个总和的<code>NSNumber</code>对象。</li><li>@avg: 把集合中的每个对象都转换为<code>double</code>类型,返回一个值为平均值的<code>NSNumber</code>对象。</li><li>@max: 使用<code>compare:</code>方法来确定最大值。所以为了让其正常工作,集合中所有的对象都必须支持和另一个对象的比较。</li><li>@min: 和 @max 一样,但是返回的是集合中的最小值。</li></ul><p>假设我们现在有一组商品数据:</p><table><thead><tr><th>Name</th><th>Price</th><th>Stock</th></tr></thead><tbody><tr><td>Apple</td><td>5</td><td>10</td></tr><tr><td>Banana</td><td>4</td><td>20</td></tr><tr><td>Peach</td><td>8</td><td>15</td></tr><tr><td>Orange</td><td>10</td><td>30</td></tr><tr><td>Strawberry</td><td>2</td><td>100</td></tr></tbody></table><p>要计算所有水果的平均价格,普通的实现方式:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">double</span> totalPrice = <span class="number">0.0</span>;</span><br><span class="line"><span class="keyword">for</span> (Goods *goods <span class="keyword">in</span> goodsArray) {</span><br><span class="line"> totalPrice += goods.price;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">double</span> averagePrice = totalPrice / goodsArray.count;</span><br></pre></td></tr></table></figure><p>而通过KVC集合操作符我们可以快速实现:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">double</span> averagePrice = [goodsArray valueForKeyPath:<span class="string">@"@avg.price"</span>]; <span class="comment">// 5.8</span></span><br></pre></td></tr></table></figure><p>其余操作符:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[goodsArray valueForKeyPath:<span class="string">@"@count"</span>]; <span class="comment">// 5</span></span><br><span class="line">[goodsArray valueForKeyPath:<span class="string">@"@avg.price"</span>]; <span class="comment">// 5.8</span></span><br><span class="line">[goodsArray valueForKeyPath:<span class="string">@"@sum.stock"</span>]; <span class="comment">// 175</span></span><br><span class="line">[goodsArray valueForKeyPath:<span class="string">@"@min.stock"</span>]; <span class="comment">// 10</span></span><br><span class="line">[goodsArray valueForKeyPath:<span class="string">@"@max.stock"</span>]; <span class="comment">// 100</span></span><br></pre></td></tr></table></figure><p><strong>对象操作符:</strong></p><ul><li>@unionOfObjects::返回一个由操作符右边的 key path 所指定的对象属性组成的数组,不会去重</li><li>distinctUnionOfObjects:与 @unionOfObjects 一样,不过会执行去重操作</li></ul><p>假设现有一个<code>Goods</code>商品数组:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSArray</span> *goods = @[Apple, Apple, Apple, Banana, Orange, Orange];</span><br></pre></td></tr></table></figure><p>执行对象操作符:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[goods valueForKeyPath:<span class="string">@"@unionOfObjects.name"</span>]; <span class="comment">// @"Apple",@"Apple",@"Apple",@"Banana",@"Orange",@"Orange"</span></span><br><span class="line">[goods valueForKeyPath:<span class="string">@"@distinctUnionOfObjects.name"</span>]; <span class="comment">// @"Apple",@"Banana",@"Orange"</span></span><br></pre></td></tr></table></figure><p><strong>数组操作符:</strong></p><ul><li>@unionOfArrays::返回了一个数组,其中包含这个集合中每个数组对于这个操作符右面指定的 key path 进行操作之后的值</li><li>@distinctUnionOfArrays:与 @unionOfArrays 一样,不过会执行去重操作</li><li>@distinctUnionOfSets: 和 @distinctUnionOfArrays 差不多, 但是它期望的是一个包含着<code>NSSet</code>对象的<code>NSSet</code>,并且会返回一个<code>NSSet</code>对象。因为集合不能包含重复的值,所以它只有<code>distinct</code>操作</li></ul><p>假设现有两个<code>Goods</code>商品数组:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSArray</span> *goods1 = @[Apple, Apple, Apple, Banana, Orange, Orange];</span><br><span class="line"><span class="built_in">NSArray</span> *goods2 = @[Banana, Peach, Peach, Orange, Strawberry];</span><br><span class="line"><span class="built_in">NSArray</span> *totalGoods = @[goods1, goods2];</span><br></pre></td></tr></table></figure><p>执行数组操作符:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[totalGoods valueForKeyPath:<span class="string">@"@unionOfArrays.name"</span>]; <span class="comment">// @"Apple",@"Apple",@"Apple",@"Banana",@"Banana",@"Peach",@"Peach",@"Orange",@"Orange",@"Orange",@"Strawberry"</span></span><br><span class="line">[totalGoods valueForKeyPath:<span class="string">@"@distinctUnionOfArrays.name"</span>]; <span class="comment">// @"Apple",@"Banana",@"Orange",@"Peach",@"Strawberry"</span></span><br></pre></td></tr></table></figure><p>参考:</p><blockquote><p><a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/index.html#//apple_ref/doc/uid/10000107i" target="_blank" rel="noopener">Key-Value Coding</a><br><a href="https://nshipster.cn/kvc-collection-operators/" target="_blank" rel="noopener">NSHipster</a></p></blockquote>]]></content>
<summary type="html">
<h1 id="KVC"><a href="#KVC" class="headerlink" title="KVC"></a>KVC</h1><p><em>Key-Value Coding(简称:KVC)</em>是苹果提供的一种可以通过<code>key</code>来直接访问
</summary>
<category term="Objective-C" scheme="http://yoursite.com/tags/Objective-C/"/>
</entry>
<entry>
<title>[Objective-C] 浅拷贝与深拷贝</title>
<link href="http://yoursite.com/2018/07/31/Objective-C-%E6%B5%85%E6%8B%B7%E8%B4%9D%E4%B8%8E%E6%B7%B1%E6%8B%B7%E8%B4%9D/"/>
<id>http://yoursite.com/2018/07/31/Objective-C-浅拷贝与深拷贝/</id>
<published>2018-07-31T07:28:14.000Z</published>
<updated>2018-08-01T08:56:40.212Z</updated>
<content type="html"><![CDATA[<p>本文内容基本都是通用的知识点,这里只是作为个人知识点的一个整理归纳,所以没有进行非常详细的讲解。</p><h1 id="浅拷贝与深拷贝"><a href="#浅拷贝与深拷贝" class="headerlink" title="浅拷贝与深拷贝"></a>浅拷贝与深拷贝</h1><p><img src="https://lh3.googleusercontent.com/-4UwIUm4Trjo/W15xw66sm7I/AAAAAAAAADA/ZlzvYYKtrXQPuBvT9A2J0_pWp9YkILwYgCHMYCw/I/15329161608785.png" alt=""><br>参考苹果文档中的图片示例可以看出,对集合对象 Array1 进行 copy,无论是浅拷贝还是深拷贝,都会复制出一份新的对象 Array2,不同之处在于:</p><ul><li>浅拷贝操作中 Array1 与 Array2 内各个元素的指针指向的还是那个对象</li><li>深拷贝中 Array1 与 Array2 内元素指针指向的是各自的对象</li></ul><h1 id="copy-与-mutableCopy"><a href="#copy-与-mutableCopy" class="headerlink" title="copy 与 mutableCopy"></a>copy 与 mutableCopy</h1><p>下面通过代码及输出信息来查看 <code>copy</code> 与 <code>mutableCopy</code> 的作用。</p><p>对<strong>不可变对象</strong>进行 <code>copy</code> 与 <code>mutableCopy</code>:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSString</span> *string = <span class="string">@"String"</span>;</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"string -> %p"</span>, string);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSString</span> *copyString = [string <span class="keyword">copy</span>];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"copyString -> %p"</span>, copyString);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSString</span> *mutableCopyString = [string mutableCopy];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"mutableCopyString -> %p"</span>, mutableCopyString);</span><br></pre></td></tr></table></figure><p>打印: </p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">string -> <span class="number">0x10e5ad110</span> </span><br><span class="line">copyString -> <span class="number">0x10e5ad110</span></span><br><span class="line">mutableCopyString -> <span class="number">0x604000444ad0</span></span><br></pre></td></tr></table></figure><p>对<strong>可变对象</strong>进行 <code>copy</code> 与 <code>mutableCopy</code>:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSMutableString</span> *mString = [<span class="built_in">NSMutableString</span> stringWithFormat:<span class="string">@"mutable String"</span>];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"mString -> %p"</span>, mString);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSString</span> *copyMutableString = [mString <span class="keyword">copy</span>];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"copyMutableString -> %p"</span>, copyMutableString);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSMutableString</span> *mutableCopyMutableString = [mString mutableCopy];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"mutableCopyMutableString -> %p"</span>, mutableCopyMutableString);</span><br></pre></td></tr></table></figure><p>打印: </p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mString -> <span class="number">0x600000255f60</span></span><br><span class="line">copyMutableString -> <span class="number">0x6040000280e0</span></span><br><span class="line">mutableCopyMutableString -> <span class="number">0x600000256110</span></span><br></pre></td></tr></table></figure><p>由此可见:</p><table><thead><tr><th></th><th>不可变对象</th><th>可变对象</th></tr></thead><tbody><tr><td>copy</td><td>无操作</td><td>深拷贝</td></tr><tr><td>mutableCopy</td><td>深拷贝</td><td>深拷贝</td></tr></tbody></table><p>不可变对象 <code>copy</code>: 不做任何拷贝操作,新建指针的值依旧是同一个指针的值</p><p>对<strong>不可变集合对象</strong>进行 <code>copy</code> 与 <code>mutableCopy</code>:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSArray</span> *array = @[<span class="string">@"a"</span>, <span class="string">@"b"</span>, <span class="string">@"c"</span>];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"array -> %p, %p, %p"</span>, array, array[<span class="number">0</span>], array[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSArray</span> *copyArray = [array <span class="keyword">copy</span>];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"copyArray -> %p, %p, %p"</span>, copyArray, copyArray[<span class="number">0</span>], copyArray[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSMutableArray</span> *mutableCopyArray = [array mutableCopy];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"mutableCopyArray -> %p, %p, %p"</span>, mutableCopyArray, mutableCopyArray[<span class="number">0</span>], mutableCopyArray[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSArray</span> *methodArray = [<span class="built_in">NSArray</span> arrayWithArray:array];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"-arrayWithArray: -> %p, %p, %p"</span>, methodArray, methodArray[<span class="number">0</span>], methodArray[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSArray</span> *copyItemsArray = [[<span class="built_in">NSArray</span> alloc] initWithArray:array copyItems:<span class="literal">YES</span>];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"-initWithArray:copyItems: -> %p, %p, %p"</span>, copyItemsArray, copyItemsArray[<span class="number">0</span>], copyItemsArray[<span class="number">1</span>]);</span><br></pre></td></tr></table></figure><p>打印: </p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">array -> <span class="number">0x6080002436f0</span>, <span class="number">0x10e5ad250</span>, <span class="number">0x10e5ad270</span></span><br><span class="line">copyArray -> <span class="number">0x6080002436f0</span>, <span class="number">0x10e5ad250</span>, <span class="number">0x10e5ad270</span></span><br><span class="line">mutableCopyArray -> <span class="number">0x600000256140</span>, <span class="number">0x10e5ad250</span>, <span class="number">0x10e5ad270</span></span><br><span class="line">-arrayWithArray: -> <span class="number">0x6000002563b0</span>, <span class="number">0x10e5ad250</span>, <span class="number">0x10e5ad270</span></span><br><span class="line">-initWithArray:copyItems: -> <span class="number">0x60c00024a800</span>, <span class="number">0x10e5ad250</span>, <span class="number">0x10e5ad270</span></span><br></pre></td></tr></table></figure><p>对<strong>可变集合对象</strong>进行 <code>copy</code> 与 <code>mutableCopy</code>:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSMutableArray</span> *mArray = [[<span class="built_in">NSMutableArray</span> alloc] initWithArray:@[<span class="string">@"1"</span>, <span class="string">@"2"</span>, <span class="string">@"3"</span>]];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"mArray -> %p, %p, %p"</span>, mArray, mArray[<span class="number">0</span>], mArray[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSArray</span> *copyMutableArray = [mArray <span class="keyword">copy</span>];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"copyMutableArray -> %p, %p, %p"</span>, copyMutableArray, copyMutableArray[<span class="number">0</span>], copyMutableArray[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSMutableArray</span> *mutableCopyMutableArray = [mArray mutableCopy];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"mutableCopyMutableArray -> %p, %p, %p"</span>, mutableCopyMutableArray, mutableCopyMutableArray[<span class="number">0</span>], mutableCopyMutableArray[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSMutableArray</span> *methodMutableArray = [<span class="built_in">NSMutableArray</span> arrayWithArray:mArray];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"-arrayWithArray: -> %p, %p, %p"</span>, methodMutableArray, methodMutableArray[<span class="number">0</span>], methodMutableArray[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSArray</span> *copyItemsMutableArray = [[<span class="built_in">NSMutableArray</span> alloc] initWithArray:mArray copyItems:<span class="literal">YES</span>];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"-initWithArray:copyItems: -> %p, %p, %p"</span>, copyItemsMutableArray, copyItemsMutableArray[<span class="number">0</span>], copyItemsMutableArray[<span class="number">1</span>]);</span><br></pre></td></tr></table></figure><p>打印: </p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">mArray -> <span class="number">0x60c00024a6b0</span>, <span class="number">0x10e5ad370</span>, <span class="number">0x10e5ad390</span></span><br><span class="line">copyMutableArray -> <span class="number">0x604000444b90</span>, <span class="number">0x10e5ad370</span>, <span class="number">0x10e5ad390</span></span><br><span class="line">mutableCopyMutableArray -> <span class="number">0x604000444e30</span>, <span class="number">0x10e5ad370</span>, <span class="number">0x10e5ad390</span></span><br><span class="line">-arrayWithArray: -> <span class="number">0x60c00024a980</span>, <span class="number">0x10e5ad370</span>, <span class="number">0x10e5ad390</span></span><br><span class="line">-initWithArray:copyItems: -> <span class="number">0x600000255b10</span>, <span class="number">0x10e5ad370</span>, <span class="number">0x10e5ad390</span></span><br></pre></td></tr></table></figure><p>由此可见:</p><table><thead><tr><th></th><th>不可变集合对象</th><th>可变集合对象</th></tr></thead><tbody><tr><td>copy</td><td>无操作</td><td>单层深拷贝</td></tr><tr><td>mutableCopy</td><td>单层深拷贝</td><td>单层深拷贝</td></tr><tr><td>-arrayWithArray:</td><td>单层深拷贝</td><td>单层深拷贝</td></tr><tr><td>-initWithArray:copyItems:</td><td>单层深拷贝</td><td>单层深拷贝</td></tr></tbody></table><ul><li>不可变集合对象 <code>copy</code>: 不做任何拷贝操作,新建指针的值依旧是同一个指针的值</li><li>其余可变、不可变集合对象的操作都是单层深拷贝</li></ul><blockquote><p>单层深拷贝:只是新建了数组对象的地址,而数组内各元素的地址依旧指向同一个对象</p></blockquote><h1 id="真正的深拷贝"><a href="#真正的深拷贝" class="headerlink" title="真正的深拷贝"></a>真正的深拷贝</h1><p>如果想进行完全的深拷贝,可以先通过 NSKeyedArchiver 将对象归档,再通过 NSKeyedUnarchiver 将对象解归档。由于在归档时,对象中每个成员变量都会收到 <code>encodeWithCoder:</code> 消息,相当于将对象所有的数据均序列化保存到磁盘上(可以看成换了种数据格式的拷贝),再通过 <code>initWithCoder:</code> 解归档时,就将拷贝过的数据经过转换后读取出来,这样就实现了深拷贝。</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[<span class="built_in">NSKeyedArchiver</span> archiveRootObject:array toFile:filePath];</span><br><span class="line"><span class="built_in">NSArray</span> *unarchiverArray = [<span class="built_in">NSKeyedUnarchiver</span> unarchiveObjectWithFile:filePath];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"array -> %p, %p, %p"</span>, array, array[<span class="number">0</span>], array[<span class="number">1</span>]);</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"unarchiverArray -> %p, %p, %p"</span>, unarchiverArray, unarchiverArray[<span class="number">0</span>], unarchiverArray[<span class="number">1</span>]);</span><br></pre></td></tr></table></figure><p>打印:</p><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">array -> <span class="number">0x6080002436f0</span>, <span class="number">0x10e5ad250</span>, <span class="number">0x10e5ad270</span></span><br><span class="line">unarchiverArray -> <span class="number">0x6000002563e0</span>, <span class="number">0xa000000000000611</span>, <span class="number">0xa000000000000621</span></span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<p>本文内容基本都是通用的知识点,这里只是作为个人知识点的一个整理归纳,所以没有进行非常详细的讲解。</p>
<h1 id="浅拷贝与深拷贝"><a href="#浅拷贝与深拷贝" class="headerlink" title="浅拷贝与深拷贝"></a>浅拷贝与深拷贝</
</summary>
<category term="Objective-C" scheme="http://yoursite.com/tags/Objective-C/"/>
</entry>
</feed>