Home > Database > Mysql Tutorial > NSArray技巧两则

NSArray技巧两则

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:36:11
Original
1431 people have browsed it

原文出自: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>
Copy after login
Copy after login
<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>
Copy after login

输出结果为:

Reverse Result

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

<span>1</span>
<span>2</span>
<span>3</span>
Copy after login
Copy after login
<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>
Copy after login

输出结果:

Result

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

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template