1 Star 0 Fork 0

Kaelinda/kaelindaTop

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
15257437237715.html 28.53 KB
一键复制 编辑 原始数据 按行查看 历史
zuosong 提交于 2019-03-12 15:13 . 2019-03-12 15:13:57
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
OC中的各种遍历 - kaelinda
</title>
<link href="atom.xml" rel="alternate" title="kaelinda" type="application/atom+xml">
<link rel="stylesheet" href="asset/css/foundation.min.css" />
<link rel="stylesheet" href="asset/css/docs.css" />
<script src="asset/js/vendor/modernizr.js"></script>
<script src="asset/js/vendor/jquery.js"></script>
<script src="asset/highlightjs/highlight.pack.js"></script>
<link href="asset/highlightjs/styles/github.css" media="screen, projection" rel="stylesheet" type="text/css">
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
function before_search(){
var searchVal = 'site:kaelinda.top ' + document.getElementById('search_input').value;
document.getElementById('search_q').value = searchVal;
return true;
}
</script>
</head>
<body class="antialiased hide-extras">
<div class="marketing off-canvas-wrap" data-offcanvas>
<div class="inner-wrap">
<nav class="top-bar docs-bar hide-for-small" data-topbar>
<section class="top-bar-section">
<div class="row">
<div style="position: relative;width:100%;"><div style="position: absolute; width:100%;">
<ul id="main-menu" class="left">
<li id=""><a target="_self" href="index.html">最近更新</a></li>
<li id=""><a target="_self" href="archives.html">归档目录</a></li>
<li id="ios.css"><a target="_self" href="ios.html">iOS</a></li>
<li id="swift.css"><a target="_self" href="swift.html">Swift</a></li>
<li id="reactnative.css"><a target="_self" href="reactnative.html">RN</a></li>
<li id="js.css"><a target="_self" href="js.html">JS</a></li>
<li id="shell.css"><a target="_self" href="shell.html">shell</a></li>
<li id="coding.css"><a target="_self" href="coding.html">编程</a></li>
<li id="live.css"><a target="_self" href="live.html">随笔</a></li>
<li id="app.css"><a target="_blank" href="app.html">APP推荐</a></li>
</ul>
<ul class="right" id="search-wrap">
<li>
<form target="_blank" onsubmit="return before_search();" action="http://google.com/search" method="get">
<input type="hidden" id="search_q" name="q" value="" />
<input tabindex="1" type="search" id="search_input" placeholder="Search"/>
</form>
</li>
</ul>
</div></div>
</div>
</section>
</nav>
<nav class="tab-bar show-for-small">
<a href="javascript:void(0)" class="left-off-canvas-toggle menu-icon">
<span> &nbsp; kaelinda</span>
</a>
</nav>
<aside class="left-off-canvas-menu">
<ul class="off-canvas-list">
<li><a target="_self" href="index.html">最近更新</a></li>
<li><a target="_self" href="archives.html">归档目录</a></li>
<li><a target="_self" href="ios.html">iOS</a></li>
<li><a target="_self" href="swift.html">Swift</a></li>
<li><a target="_self" href="reactnative.html">RN</a></li>
<li><a target="_self" href="js.html">JS</a></li>
<li><a target="_self" href="shell.html">shell</a></li>
<li><a target="_self" href="coding.html">编程</a></li>
<li><a target="_self" href="live.html">随笔</a></li>
<li><a target="_blank" href="app.html">APP推荐</a></li>
<li><label>Categories</label></li>
<li><a href="reactnative.html">React-Native</a></li>
<li><a href="ios.html">iOS</a></li>
<li><a href="app.html">MacAPP</a></li>
<li><a href="swift.html">Swift</a></li>
<li><a href="js.html">JavaScript</a></li>
<li><a href="vue.html">Vue</a></li>
<li><a href="shell.html">shell</a></li>
<li><a href="%E7%BD%91%E7%BB%9C.html">网络</a></li>
</ul>
</aside>
<a class="exit-off-canvas" href="#"></a>
<section id="main-content" role="main" class="scroll-container">
<script type="text/javascript">
$(function(){
$('#menu_item_index').addClass('is_active');
});
</script>
<div class="row">
<div class="large-8 medium-8 columns">
<div class="markdown-body article-wrap">
<div class="article">
<h1>OC中的各种遍历</h1>
<div class="read-more clearfix">
<span class="date">2018/5/8</span>
<span>posted in&nbsp;</span>
<span class="posted-in"><a href='ios.html'>iOS</a></span>
<span class="comments">
</span>
</div>
</div><!-- article -->
<div class="article-content">
<blockquote>
<p>iOS开发中我们经常遇到各种的数据处理,所以就会经常遇到数组遍历 和 字典遍历。但是不同的遍历方式,效率不同,并且还有不同的限制。</p>
</blockquote>
<p>首先把数据源准备好:</p>
<pre><code class="language-text">_dataSource = [NSMutableArray array];
_dataDic = [NSMutableDictionary dictionary];
for (int i=0; i&lt;5; i++) {
[_dataSource addObject:@(i)];
[_dataDic setObject:@(i) forKey:[NSString stringWithFormat:@&quot;我是第%d个Item&quot;,i]];
}
</code></pre>
<span id="more"></span><!-- more -->
<h2 id="toc_0">for循环遍历</h2>
<h3 id="toc_1">遍历数组</h3>
<pre><code class="language-text">for (int i = 0; i&lt;_dataSource.count; i++) {
NSLog(@&quot;%@ --- thread:%@&quot;,[_dataSource objectAtIndex:i],[NSThread currentThread]);
}
</code></pre>
<h3 id="toc_2">遍历字典</h3>
<pre><code class="language-text">// 取出字典中所有的key (无序的)
NSArray *keys = [_dataDic allKeys];
for (int i = 0; i&lt;keys.count; i++) {
// 根据key值索引Value即可
NSString *key = [keys objectAtIndex:i];
NSString *value = [_dataDic objectForKey:key];
NSLog(@&quot;%@ --- thread:%@&quot;,value,[NSThread currentThread]);
}
</code></pre>
<p>这是打印结果</p>
<pre><code class="language-text">2018-05-09 15:01:23.119707+0800 OCProject[78848:4457173] 0 --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
2018-05-09 15:01:23.120003+0800 OCProject[78848:4457173] 1 --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
2018-05-09 15:01:23.120262+0800 OCProject[78848:4457173] 2 --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
2018-05-09 15:01:23.120473+0800 OCProject[78848:4457173] 3 --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
2018-05-09 15:01:23.120754+0800 OCProject[78848:4457173] 4 --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
2018-05-09 15:01:23.120951+0800 OCProject[78848:4457173] 我是第3个Item --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
2018-05-09 15:01:23.121111+0800 OCProject[78848:4457173] 我是第1个Item --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
2018-05-09 15:01:23.121231+0800 OCProject[78848:4457173] 我是第4个Item --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
2018-05-09 15:01:23.121377+0800 OCProject[78848:4457173] 我是第2个Item --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
2018-05-09 15:01:23.121539+0800 OCProject[78848:4457173] 我是第0个Item --- thread:&lt;NSThread: 0x604000263b40&gt;{number = 1, name = main}
</code></pre>
<h3 id="toc_3">【分析】</h3>
<ul>
<li>优点:简单</li>
<li>缺点:遍历字典有太多中间变量,会有一部分开销</li>
</ul>
<h2 id="toc_4">for-in遍历</h2>
<h3 id="toc_5">遍历数组</h3>
<pre><code class="language-text">// 遍历数组
for (NSNumber *obj in _dataSource) {
NSLog(@&quot;%@ --- thread:%@&quot;,obj,[NSThread currentThread]);
}
</code></pre>
<h3 id="toc_6">遍历字典</h3>
<pre><code class="language-text">// 遍历字典
NSArray *keys = [_dataDic allKeys];
for (NSString *key in keys) {
NSString *value = [_dataDic objectForKey:key];
NSLog(@&quot;%@ --- thread:%@&quot;,value,[NSThread currentThread]);
}
</code></pre>
<p>打印结果:</p>
<pre><code class="language-text">2018-05-09 15:17:28.993429+0800 OCProject[79116:4479093] 0 --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
2018-05-09 15:17:28.993769+0800 OCProject[79116:4479093] 1 --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
2018-05-09 15:17:28.993980+0800 OCProject[79116:4479093] 2 --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
2018-05-09 15:17:28.994204+0800 OCProject[79116:4479093] 3 --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
2018-05-09 15:17:28.994375+0800 OCProject[79116:4479093] 4 --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
2018-05-09 15:17:28.994505+0800 OCProject[79116:4479093] 我是第3个Item --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
2018-05-09 15:17:28.994693+0800 OCProject[79116:4479093] 我是第1个Item --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
2018-05-09 15:17:28.994861+0800 OCProject[79116:4479093] 我是第4个Item --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
2018-05-09 15:17:28.995037+0800 OCProject[79116:4479093] 我是第2个Item --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
2018-05-09 15:17:28.995253+0800 OCProject[79116:4479093] 我是第0个Item --- thread:&lt;NSThread: 0x60400006c0c0&gt;{number = 1, name = main}
</code></pre>
<blockquote>
<p>这里需要注意的是的 for-in 遍历时如果对数组进行增删操作会导致程序crash。</p>
</blockquote>
<h3 id="toc_7">【分析】</h3>
<ul>
<li>优点:
<ul>
<li>语法简洁</li>
<li>效率高</li>
</ul></li>
<li>缺点:无法获得当前遍历操作所针对的下标。</li>
</ul>
<h2 id="toc_8">枚举器</h2>
<h3 id="toc_9">遍历数组</h3>
<pre><code class="language-text">/*
* 正向枚举:objectEnumerator
* 反向枚举:reverseObjectEnumerator
*/
NSEnumerator *enumetorArr = [_dataSource objectEnumerator];
id obj ;
while (obj=[enumetorArr nextObject]) {
NSLog(@&quot;%@ --- thread:%@&quot;,obj,[NSThread currentThread]);
}
</code></pre>
<h3 id="toc_10">遍历字典</h3>
<pre><code class="language-text"> NSEnumerator *enumtorValue = [_dataDic objectEnumerator];// Value枚举器 reverseObjectEnumerator反向顺序
NSEnumerator *enumtorKey = [_dataDic keyEnumerator];// key 枚举器
id value;
while (value=[enumtorDic nextObject]) {
NSLog(@&quot;%@ --- thread:%@&quot;,value,[NSThread currentThread]);
}
</code></pre>
<p>打印结果:</p>
<pre><code class="language-text">2018-05-09 15:35:49.961318+0800 OCProject[79558:4515099] 0 --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
2018-05-09 15:35:49.961554+0800 OCProject[79558:4515099] 1 --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
2018-05-09 15:35:49.961666+0800 OCProject[79558:4515099] 2 --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
2018-05-09 15:35:49.961788+0800 OCProject[79558:4515099] 3 --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
2018-05-09 15:35:49.961909+0800 OCProject[79558:4515099] 4 --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
2018-05-09 15:35:49.962030+0800 OCProject[79558:4515099] 我是第3个Item --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
2018-05-09 15:35:49.962130+0800 OCProject[79558:4515099] 我是第1个Item --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
2018-05-09 15:35:49.962257+0800 OCProject[79558:4515099] 我是第4个Item --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
2018-05-09 15:35:49.962369+0800 OCProject[79558:4515099] 我是第2个Item --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
2018-05-09 15:35:49.962485+0800 OCProject[79558:4515099] 我是第0个Item --- thread:&lt;NSThread: 0x604000065300&gt;{number = 1, name = main}
</code></pre>
<h3 id="toc_11">【分析】</h3>
<ul>
<li><p>优点:对于不同的数据类型,遍历的语法相似;内部可以简单的通过reverseObjectEnumerator设置进行反向遍历。</p></li>
<li><p>缺点:代码量稍大。</p></li>
</ul>
<h2 id="toc_12">快速枚举遍历</h2>
<h3 id="toc_13">遍历数组</h3>
<pre><code class="language-text">// 按照顺序来的
[_dataSource enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@&quot;sort:0 %@ -- %lu --- %@ --- %@&quot;,obj,(unsigned long)idx,stop?@&quot;YES&quot;:@&quot;NO&quot;,[NSThread currentThread]);
}];
// 随机的顺序遍历,开启了多个子线程 NSEnumerationReverse反向遍历,并发遍历 NSEnumerationConcurrent 开启了多个子线程
[_dataSource enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@&quot;sort:1 %@ -- %lu --- %@ --- %@&quot;,obj,(unsigned long)idx,stop?@&quot;YES&quot;:@&quot;NO&quot;,[NSThread currentThread]);
}];
// 指定循环范围 ,循环次数多可以开辟子线程
[_dataSource enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, _dataSource.count)] options:NSEnumerationConcurrent usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@&quot;sort:2 %@ -- %lu --- %@ --- %@&quot;,obj,(unsigned long)idx,stop?@&quot;YES&quot;:@&quot;NO&quot;,[NSThread currentThread]);
}];
</code></pre>
<p>打印结果:</p>
<pre><code class="language-text">2018-05-09 18:31:25.808197+0800 OCProject[923:284717] sort:0 0 -- 0 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.808692+0800 OCProject[923:284717] sort:0 1 -- 1 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.808975+0800 OCProject[923:284717] sort:0 2 -- 2 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.809250+0800 OCProject[923:284717] sort:0 3 -- 3 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.809499+0800 OCProject[923:284717] sort:0 4 -- 4 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.809890+0800 OCProject[923:284717] sort:1 0 -- 0 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.810140+0800 OCProject[923:284717] sort:1 1 -- 1 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.810388+0800 OCProject[923:284717] sort:1 2 -- 2 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.810635+0800 OCProject[923:284717] sort:1 3 -- 3 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.810883+0800 OCProject[923:284717] sort:1 4 -- 4 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.813959+0800 OCProject[923:284717] sort:2 0 -- 0 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.814289+0800 OCProject[923:284717] sort:2 1 -- 1 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.814552+0800 OCProject[923:284717] sort:2 2 -- 2 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.814806+0800 OCProject[923:284717] sort:2 3 -- 3 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
2018-05-09 18:31:25.815054+0800 OCProject[923:284717] sort:2 4 -- 4 --- YES --- &lt;NSThread: 0x137e0bb50&gt;{number = 1, name = main}
</code></pre>
<p>当我把数组内的元素个数增加到 10,请看前缀是<code>sort:1</code> 的打印结果如下:</p>
<pre><code class="language-text">2018-05-09 18:34:41.806499+0800 OCProject[928:286028] sort:1 0 -- 0 --- YES --- &lt;NSThread: 0x145e0bb50&gt;{number = 1, name = main}
2018-05-09 18:34:41.806982+0800 OCProject[928:286068] sort:1 1 -- 1 --- YES --- &lt;NSThread: 0x145eb08d0&gt;{number = 3, name = (null)}
2018-05-09 18:34:41.809488+0800 OCProject[928:286068] sort:1 3 -- 3 --- YES --- &lt;NSThread: 0x145eb08d0&gt;{number = 3, name = (null)}
2018-05-09 18:34:41.809518+0800 OCProject[928:286028] sort:1 2 -- 2 --- YES --- &lt;NSThread: 0x145e0bb50&gt;{number = 1, name = main}
2018-05-09 18:34:41.809788+0800 OCProject[928:286068] sort:1 4 -- 4 --- YES --- &lt;NSThread: 0x145eb08d0&gt;{number = 3, name = (null)}
2018-05-09 18:34:41.809840+0800 OCProject[928:286028] sort:1 5 -- 5 --- YES --- &lt;NSThread: 0x145e0bb50&gt;{number = 1, name = main}
2018-05-09 18:34:41.810045+0800 OCProject[928:286068] sort:1 6 -- 6 --- YES --- &lt;NSThread: 0x145eb08d0&gt;{number = 3, name = (null)}
2018-05-09 18:34:41.810462+0800 OCProject[928:286028] sort:1 7 -- 7 --- YES --- &lt;NSThread: 0x145e0bb50&gt;{number = 1, name = main}
2018-05-09 18:34:41.810989+0800 OCProject[928:286068] sort:1 8 -- 8 --- YES --- &lt;NSThread: 0x145eb08d0&gt;{number = 3, name = (null)}
2018-05-09 18:34:41.811034+0800 OCProject[928:286028] sort:1 9 -- 9 --- YES --- &lt;NSThread: 0x145e0bb50&gt;{number = 1, name = main}
</code></pre>
<h3 id="toc_14">遍历字典</h3>
<pre><code class="language-text">// 主线程 无序
[_dataDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@&quot;key:%@ -- value:%@ --- %@&quot;,key,obj,[NSThread currentThread]);
}];
// 无序; NSEnumerationReverse反向遍历,并发遍历 NSEnumerationConcurrent
[_dataDic enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@&quot;key:%@ -- value:%@ --- %@&quot;,key,obj,[NSThread currentThread]);
}];
</code></pre>
<p>打印结果:</p>
<pre><code class="language-text">2018-05-09 18:33:48.223268+0800 OCProject[926:285509] key:3 -- value:我是第3个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
2018-05-09 18:33:48.224066+0800 OCProject[926:285509] key:1 -- value:我是第1个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
2018-05-09 18:33:48.225122+0800 OCProject[926:285509] key:4 -- value:我是第4个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
2018-05-09 18:33:48.226368+0800 OCProject[926:285509] key:2 -- value:我是第2个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
2018-05-09 18:33:48.226915+0800 OCProject[926:285509] key:0 -- value:我是第0个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
2018-05-09 18:33:48.227419+0800 OCProject[926:285509] key:3 -- value:我是第3个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
2018-05-09 18:33:48.228593+0800 OCProject[926:285509] key:1 -- value:我是第1个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
2018-05-09 18:33:48.229978+0800 OCProject[926:285509] key:4 -- value:我是第4个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
2018-05-09 18:33:48.231440+0800 OCProject[926:285509] key:2 -- value:我是第2个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
2018-05-09 18:33:48.231977+0800 OCProject[926:285509] key:0 -- value:我是第0个Item --- &lt;NSThread: 0x107e0b250&gt;{number = 1, name = main}
</code></pre>
<h3 id="toc_15">【分析】</h3>
<ul>
<li>优点:
<ul>
<li>遍历时可以直接从block中获得需要的所有信息,包括下标、值等。特别相对于字典而言,不需要做多余的编码即可同时获得key和value的值。</li>
<li>能够直接修改block中key或者obj的类型为真实类型,可以省去类型转换的工作。</li>
<li>可以通过NSEnumerationConcurrent枚举值开启并发迭代功能。</li>
</ul></li>
<li> 缺点:暂无<br/>
&gt; 说明:基于Block的遍历方式在实现反向遍历的时候也非常简单,使用enumerateObjectsWithOptions方法,传递NSEnumerationReverse作为参数即可,在处理遍历操作的时候推荐基于Block的遍历方式。</li>
</ul>
<h2 id="toc_16">迭代</h2>
<h3 id="toc_17">遍历数组</h3>
<pre><code class="language-text">dispatch_queue_t queue =dispatch_queue_create(&quot;apply并行队列&quot;, DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(_dataSource.count, queue, ^(size_t index) {
NSLog(@&quot;%@----%@&quot;,self-&gt;_dataSource[index],[NSThread currentThread]);
});
NSLog(@&quot;end1&quot;);
</code></pre>
<h3 id="toc_18">遍历字典</h3>
<pre><code class="language-text">NSArray *allkeys = [_dataDic allKeys];
NSArray *allValue = [_dataDic allValues];
dispatch_apply(_dataDic.count, queue, ^(size_t index) {
NSString *key = [allkeys objectAtIndex:index];
NSLog(@&quot;dic: %@----%@&quot;,[self-&gt;_dataDic objectForKey:key],[NSThread currentThread]);
});
</code></pre>
<p>打印结果:</p>
<pre><code class="language-text">2018-05-09 19:24:03.875390+0800 OCProject[975:301673] 0----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.875913+0800 OCProject[975:301673] 2----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.876194+0800 OCProject[975:301673] 3----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.876445+0800 OCProject[975:301673] 4----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.876688+0800 OCProject[975:301673] 5----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.876926+0800 OCProject[975:301673] 6----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.877162+0800 OCProject[975:301673] 7----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.877398+0800 OCProject[975:301673] 8----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.877669+0800 OCProject[975:301673] 9----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.878021+0800 OCProject[975:301852] 1----&lt;NSThread: 0x1231b4750&gt;{number = 3, name = (null)}
2018-05-09 19:24:03.878310+0800 OCProject[975:301673] end1
2018-05-09 19:24:03.878752+0800 OCProject[975:301673] dic: 我是第7个Item----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.879072+0800 OCProject[975:301673] dic: 我是第3个Item----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.879372+0800 OCProject[975:301673] dic: 我是第8个Item----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.879672+0800 OCProject[975:301673] dic: 我是第4个Item----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.879971+0800 OCProject[975:301673] dic: 我是第0个Item----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.880271+0800 OCProject[975:301673] dic: 我是第9个Item----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.880568+0800 OCProject[975:301673] dic: 我是第5个Item----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.883701+0800 OCProject[975:301673] dic: 我是第6个Item----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.883947+0800 OCProject[975:301673] dic: 我是第2个Item----&lt;NSThread: 0x121e0ba50&gt;{number = 1, name = main}
2018-05-09 19:24:03.880890+0800 OCProject[975:301852] dic: 我是第1个Item----&lt;NSThread: 0x1231b4750&gt;{number = 3, name = (null)}
</code></pre>
<h3 id="toc_19">【分析】</h3>
<ul>
<li>优点:开启多条线程并发处理遍历任务,执行效率高。</li>
<li>缺点:
<ul>
<li>对于字典和集合的处理需借助数组 </li>
<li>无法实现反向遍历。</li>
</ul></li>
</ul>
</div>
<div class="row">
<div class="large-6 columns">
<p class="text-left" style="padding:15px 0px;">
<a href="15261167596297.html"
title="Previous Post: OC 多线程之 GCD知识从基础到进阶 (1)">&laquo; OC 多线程之 GCD知识从基础到进阶 (1)</a>
</p>
</div>
<div class="large-6 columns">
<p class="text-right" style="padding:15px 0px;">
<a href="15253400374602.html"
title="Next Post: 多线程 之 NSThread">多线程 之 NSThread &raquo;</a>
</p>
</div>
</div>
<div class="comments-wrap">
<div class="share-comments">
</div>
</div>
</div><!-- article-wrap -->
</div><!-- large 8 -->
<div class="large-4 medium-4 columns">
<div class="hide-for-small">
<div id="sidebar" class="sidebar">
<div id="site-info" class="site-info">
<div class="site-a-logo"><img src="https://ws1.sinaimg.cn/large/006tNc79ly1fmpk9088gsj30sg0sgn33.jpg" /></div>
<h1>kaelinda</h1>
<div class="site-des">倘若我心中的山水,你眼中能看到,我便一步一莲花祈祷</div>
<div class="social">
<a target="_blank" class="twitter" target="_blank" href="https://twitter.com/KaelLVLinda" title="Twitter">Twitter</a>
<a target="_blank" class="github" target="_blank" href="https://github.com/Kaelzzs" title="GitHub">GitHub</a>
<a target="_blank" class="email" href="mailto:zhouzuosong_kael@163.com" title="Email">Email</a>
<a target="_blank" class="rss" href="atom.xml" title="RSS">RSS</a>
</div>
</div>
<div id="site-categories" class="side-item ">
<div class="side-header">
<h2>Categories</h2>
</div>
<div class="side-content">
<p class="cat-list">
<a href="reactnative.html"><strong>React-Native</strong></a>
<a href="ios.html"><strong>iOS</strong></a>
<a href="app.html"><strong>MacAPP</strong></a>
<a href="swift.html"><strong>Swift</strong></a>
<a href="js.html"><strong>JavaScript</strong></a>
<a href="vue.html"><strong>Vue</strong></a>
<a href="shell.html"><strong>shell</strong></a>
<a href="%E7%BD%91%E7%BB%9C.html"><strong>网络</strong></a>
</p>
</div>
</div>
<div id="site-categories" class="side-item">
<div class="side-header">
<h2>Recent Posts</h2>
</div>
<div class="side-content">
<ul class="posts-list">
<li class="post">
<a href="15523077857608.html">OC中枚举写法 以及 字符串枚举探索</a>
</li>
<li class="post">
<a href="15522686353535.html">iOS 动画全解 (1)</a>
</li>
<li class="post">
<a href="15501356584731.html">UITableView顶部空白的几种解决办法</a>
</li>
<li class="post">
<a href="15488351784332.html">Objective-C中的字符串类型枚举 探索</a>
</li>
<li class="post">
<a href="15409691048860.html">【Swift笔记】引用计数</a>
</li>
</ul>
</div>
</div>
</div><!-- sidebar -->
</div><!-- hide for small -->
</div><!-- large 4 -->
</div><!-- row -->
<div class="page-bottom clearfix">
<div class="row">
<p class="copyright">Copyright &copy; 2015
Powered by <a target="_blank" href="http://www.mweb.im">MWeb</a>,&nbsp;
Theme used <a target="_blank" href="http://github.com">GitHub CSS</a>.</p>
</div>
</div>
</section>
</div>
</div>
<script src="asset/js/foundation.min.js"></script>
<script>
$(document).foundation();
function fixSidebarHeight(){
var w1 = $('.markdown-body').height();
var w2 = $('#sidebar').height();
if (w1 > w2) { $('#sidebar').height(w1); };
}
$(function(){
fixSidebarHeight();
})
$(window).load(function(){
fixSidebarHeight();
});
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({TeX: { equationNumbers: { autoNumber: "AMS" } }});</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kaelinda/kaelindaTop.git
git@gitee.com:kaelinda/kaelindaTop.git
kaelinda
kaelindaTop
kaelindaTop
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385