NSArray技巧两则

WBOY
Lepaskan: 2016-06-07 15:36:11
asal
1386 orang telah melayarinya

原文出自:http://nijino.cn/blog/2014/01/05/nsarray-skills/ 之前我在网易的技术博客中讲到去除NSArray中重复元素的方法。最近在看WWDC2013视频,发现苹果工程师对数组去重有更为简便的方法,还有一个对数组逆序的简便方法,赶紧记录下来备用。 NSArray逆

原文出自:http://nijino.cn/blog/2014/01/05/nsarray-skills/


之前我在网易的技术博客中讲到去除NSArray中重复元素的方法。最近在看WWDC2013视频,发现苹果工程师对数组去重有更为简便的方法,还有一个对数组逆序的简便方法,赶紧记录下来备用。

  1. NSArray逆序
Reverse sequence of NSArray

<span>1</span>
<span>2</span>
<span>3</span>
Salin selepas log masuk
Salin selepas log masuk
<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>
Salin selepas log masuk

输出结果为:

Reverse Result

<span>1</span>
<span>2</span>
<span>3</span>
4
Salin selepas log masuk
Salin selepas log masuk
<code><span>2014-01-05 13:52:09.303 CircleAudio[39584:70b] (
</span><span>second,
</span><span>first
</span><span>)</span></code>
Salin selepas log masuk
  1. NSArray快速去重
Make NSArray distinct

<span>1</span>
<span>2</span>
<span>3</span>
Salin selepas log masuk
Salin selepas log masuk
<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>
Salin selepas log masuk

输出结果:

Result

<span>1</span>
<span>2</span>
<span>3</span>
4
Salin selepas log masuk
Salin selepas log masuk
<code><span>2014-01-05 13:54:53.457 CircleAudio[39976:70b] (
</span><span>first,
</span><span>second
</span><span>)</span></code>
Salin selepas log masuk

以上的技巧都是从WWDC2013 session 228中学到的,其中还有一些技巧比如:联合两个数组并去重,感兴趣的同学可以自行下载视频研究。


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan