iOS数组遍历
对于一个数组
NSArray *array = @[@"111",@"222",@"333",@"444",@"555",@"666",@"777",@"888",@"999",];
NSInteger count =array.count;
1.for循环
for (NSInteger i=0; i<count; i++) {
NSLog(@"%@----%@",array[i],[NSThread currentThread]);
}
2.for in快速枚举
for (NSString *string in array) {
NSLog(@"%@----%@",string,[NSThread currentThread]);
}
集合中对象数很多的情况下,for in 的遍历速度非常之快。但小规模的遍历 还没for循环快。
3. 枚举器NSEnumerator
// 向数组请求枚举器
NSEnumerator *enumer = [array objectEnumerator]; // 正序
NSEnumerator *enumer2 = [array reverseObjectEnumerator]; // 倒序
id obj;
while ( obj = [enumer nextObject]) {
// nextObject为nil,结束循环
// 不可对array的元素进行增 删
NSLog(@"%@----%@",obj,[NSThread currentThread]);
}
4. enumerateObjectsUsingBlock方法
// 顺序遍历
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@----%@",array[idx],[NSThread currentThread]);
if (idx == 5) {
*stop = YES; // 停止遍历
}
}];
// 倒序遍历
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@----%@",array[idx],[NSThread currentThread]);
if (idx == 5) {
*stop = YES; // 停止遍历
}
}];
Block内代码可以并发执行。
字典情况下
NSDictionary * dic = [NSDictionary dictionary];
[dic enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
NSLog(@"value for key %@ is %@ ", key, value);
if ([@"key2" isEqualToString:key]) {
*stop = YES;
}
}];
遍历字典类型时,推荐使用。字典遍历可以同时取allkeys和allvalues中的元素。
5.多线程dispatch_apply
// 并行队列
dispatch_queue_t queue = dispatch_queue_create("zzz", DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(count, queue, ^(size_t index) {
NSLog(@"%@----%@",array[index],[NSThread currentThread]);
});
适用于 处理每一次循环都有耗时任务的数组遍历。
6.NSSet
NSArray *arr1 = ....;
NSArray *arr2 =....;
NSSet *aaaSet = [NSSet setWithArray:arr2];
for (NSUInteger i = 0; i < arr1.count; ++i) {
UIView *targetView = arr1[i];
if ([aaaSet containsObject:targetView]) {
//....
}
}
iOS数组遍历的更多相关文章
- iOS中数组遍历的方法及比较
数组遍历是编码中很常见的一种需求,我们来扒一拔iOS里面都有什么样的方法来实现,有什么特点. 因为ios是兼容C语言的,所以c语言里面的最最常见的for循环遍历是没有问题的. 本文中用的数组是获取的系 ...
- iOS中数组遍历的方法及比較
数组遍历是编码中非经常见的一种需求.我们来扒一拔iOS里面都有什么样的方法来实现,有什么特点. 由于iOS是兼容C语言的.所以C语言里面的最最常见的for循环遍历是没有问题的. 本文中用的数组是获取的 ...
- [BS-02] iOS数组、字典、NSNumber 新写法—— @[]、@{}
IOS数组.字典.NSNumber 新写法—— @[].@{} //标准写法 NSNumber * number = [NSNumber numberWithInt:]; NSArray * ar ...
- 转→js数组遍历 千万不要使用for...in...
看到一篇内容还不错,但是排版实在糟糕, 逼死强迫症患者啊,直接拉下去找原文连接,找到了,但是已经消失了···500错误... 第一次因为实在看不下去一篇博客的排版, 为了排版而转载... 转载地址:h ...
- (转)深入理解PHP之数组(遍历顺序)
深入理解PHP之数组(遍历顺序)(转) http://www.laruence.com/2009/08/23/1065.html (鸟哥) 经常会有人问我, PHP的数组, 如果用foreach来访问 ...
- JS几种数组遍历方式以及性能分析对比
前言 这一篇与上一篇 JS几种变量交换方式以及性能分析对比 属于同一个系列,本文继续分析JS中几种常用的数组遍历方式以及各自的性能对比 起由 在上一次分析了JS几种常用变量交换方式以及各自性能后,觉得 ...
- iOS数组使用
相关链接: ios数组基本用法和排序 NSArray 排序汇总 iOS 数组排序方法 IOS-筛选数组内的元素 关于EnumerateObjectsUsingBlock和for-in之间的较量 [iO ...
- C# for和 foreach 的数组遍历 比较
刚学习程序,感觉写代码 很有意思,所以把自己的感悟写下来啦,第一次写博客,可能是菜鸟中的菜鸟 时间久了,相信就会写的很好哦! for和 foreach 的数组遍历 比较 很简单的程序,不解释啦! u ...
- JS之对象数组遍历?
一.js实现遍历对象 <script> ","destroy":"97%"}; var props = ""; for ...
随机推荐
- CEF拦截js层alert弹窗 OnJSDialog 《转》
一 引言 CEF3嵌入后,用JS 弹出Alert框,按钮错位,确定按钮勉强能看到.很难看.为了改善体验,决定重写提示框. 环境:VS2008 VC MFC. 二 原理 参看类 CefJSDia ...
- Python script to package the information of tracking benchmarks like LaSOT and GOT-10k into json files for Siamese network based trackers
############################################################################################ #### Fo ...
- 012 spring retry重试原理的解析
有点复杂,在后续的章节,将会对其中涉及到的知识点,再分章节进行说明. 1.程序结构 2.@Retryable package com.jun.web.annotation.theory; import ...
- Python3基础 yield 创建生成器
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- 泡泡一分钟:Using Geometric Features to Represent Near-Contact Behavior in Robotic Grasping
张宁 Using Geometric Features to Represent Near-Contact Behavior in Robotic Grasping链接:https://pan.ba ...
- matlab学习笔记9 高级绘图命令_2 图形的高级控制_视点控制和图形旋转_色图和颜色映像_光照和着色
一起来学matlab-matlab学习笔记9 高级绘图命令_2 图形的高级控制_视点控制和图形旋转_色图和颜色映像_光照和着色 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 < ...
- Delphi ADOConnection连接
unit Unit_DM; interface usesSysUtils, Classes, DB, ADODB,inifiles,windows,forms,controls; typeTDM = ...
- [LeetCode] 507. Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- python jieba
https://www.cnblogs.com/jiayongji/p/7119065.html 安装 pip install jieba 简单用法 结巴分词分为三种模式:精确模式(默认).全模式和搜 ...
- react-redux源码学习
React-redux 源码学习 version 7.0.3 目录 Provider connect mapStateToProps mapDispatchToProps mergeProps opt ...