Home  >  Article  >  Database  >  NSArray技巧两则

NSArray技巧两则

WBOY
WBOYOriginal
2016-06-07 15:36:111394browse

原文出自: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>
<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>
  1. NSArray快速去重
Make NSArray distinct

<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中学到的,其中还有一些技巧比如:联合两个数组并去重,感兴趣的同学可以自行下载视频研究。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:给log4j配置数据库连接Next article:eclipse调试技巧