Maison > Article > base de données > NSArray技巧两则
原文出自:http://nijino.cn/blog/2014/01/05/nsarray-skills/ 之前我在网易的技术博客中讲到去除NSArray中重复元素的方法。最近在看WWDC2013视频,发现苹果工程师对数组去重有更为简便的方法,还有一个对数组逆序的简便方法,赶紧记录下来备用。 NSArray逆
原文出自:http://nijino.cn/blog/2014/01/05/nsarray-skills/
之前我在网易的技术博客中讲到去除NSArray中重复元素的方法。最近在看WWDC2013视频,发现苹果工程师对数组去重有更为简便的方法,还有一个对数组逆序的简便方法,赶紧记录下来备用。
<span>1</span> <span>2</span> <span>3</span> |
<code><span><span>NSArray</span> <span>*</span><span>arr</span> <span>=</span> <span>@</span><span>[</span><span>@"first"</span><span>,</span><span>@"second"</span><span>];</span> </span><span><span>arr</span> <span>=</span> <span>arr</span><span>.</span><span>reverseObjectEnumerator</span><span>.</span><span>allObjects</span><span>;</span> </span><span><span>NSLog</span><span>(</span><span>@"%@"</span><span>,</span><span>arr</span><span>);</span> </span></code> |
输出结果为:
Reverse Result
<span>1</span>
<span>2</span>
<span>3</span>
4
|
<code><span>2014-01-05 13:52:09.303 CircleAudio[39584:70b] ( </span><span>second, </span><span>first </span><span>)</span></code> |
<span>1</span> <span>2</span> <span>3</span> |
<code><span><span>NSArray</span> <span>*</span><span>arr</span> <span>=</span> <span>@</span><span>[</span><span>@"first"</span><span>,</span><span>@"second"</span><span>,</span><span>@"first"</span><span>];</span> </span><span><span>arr</span> <span>=</span> <span>[</span><span>arr</span> <span>valueForKeyPath:</span><span>@"@distinctUnionOfObjects.self"</span><span>];</span> </span><span><span>NSLog</span><span>(</span><span>@"%@"</span><span>,</span><span>arr</span><span>);</span> </span></code> |
输出结果:
Result
<span>1</span>
<span>2</span>
<span>3</span>
4
|
<code><span>2014-01-05 13:54:53.457 CircleAudio[39976:70b] ( </span><span>first, </span><span>second </span><span>)</span></code> |
以上的技巧都是从WWDC2013 session 228中学到的,其中还有一些技巧比如:联合两个数组并去重,感兴趣的同学可以自行下载视频研究。