当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS中遍历的方法总结

iOS中遍历的方法总结

2019年07月24日  | 移动技术网IT编程  | 我要评论

按摩女郎之彩虹某处,金华烟草电子商务网,草配药禁忌

在ios开发中,可以使用多种方法进行元素遍历,具体有一下几种:

经典for循环

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];
for (int i = 0; i < iosarray.count; i++) {
  //处理数组中数据
  nslog(@"%@", iosarray[i]);
}

nsenumerator遍历

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];
nsenumerator *enumerator = [iosarray objectenumerator];//正向遍历
// nsenumerator *enumerator = [iosarray reverseobjectenumerator];//反向遍历

id object;

while ((object = [enumerator nextobject]) != nil) {
  //处理枚举器中的数据
  nslog(@"%@", object);
}

for-in快速遍历

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];
for (nsstring *obj in iosarray) {
  //处理数组中的数据
  nslog(@"%@", obj);
}

enumeratorblock遍历

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];
[iosarray enumerateobjectsusingblock:^(id _nonnull obj, nsuinteger idx, bool * _nonnull stop) {
  nslog(@"%@", obj);
  if ([obj isequaltostring:@"e"]) {
    *stop = yes;  // 跳出遍历
  }
}];

另外,enumeratorblock还支持反向遍历,并发遍历,并发遍历可以使用多核的优化,充分利用系统的资源。

反向遍历

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];
[iosarray enumerateobjectswithoptions:nsenumerationreverse usingblock:^(nsstring *obj, nsuinteger idx, bool * _nonnull stop) {
  nslog(@"%@", obj);
  if ([obj isequaltostring:@"e"]) {
    *stop = yes;
  }
}];

并发遍历

nsarray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];
nsmutablearray *iosmutablearray = [nsmutablearray arraywitharray:iosarray];
[iosmutablearray enumerateobjectswithoptions:nsenumerationconcurrent usingblock:^(nsstring *obj, nsuinteger idx, bool * _nonnull stop) {
  obj = [nsstring stringwithformat:@"_%@", obj];
  [iosmutablearray replaceobjectatindex:idx withobject:obj];
  nslog(@"%@", obj);

  if ([obj isequaltostring:@"_i"]) {
    *stop = yes;
  }
}];

dispatch_apply遍历

dispatch_apply类似于for循环,这里需要注意的是,dispatch_apple是同步调用,调用完毕返回结果,并且由于是gcd实现,所以可以使用并发队列或者是串行队列。代码如下:

dispatch_queue_t queue = dispatch_queue_create("queue", dispatch_queue_concurrent);
//  dispatch_queue_t queue = dispatch_queue_create("queue", dispatch_queue_serial); // 串行队列
dispatch_apply(array.count, queue, ^(size_t i) {
  enumerate *enumerate = [array objectatindex:i];
  nslog(@"number: %ld", enumerate.number);
});

遍历的注意事项

for循环中不要修改数组

遍历过程中是不能随便删除遍历的元素的,如果需要删除元素,可以先复制一份出来,比如如下的代码会有问题:

nsmutablearray *iosarray = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g"];
for (nsstring *obj in iosarray) {
  //处理数组中的数据
  if([@"e" isequalto:obj]) {
    [iosarray removeobject:obj];
  }
}

但是使用enumerateblock可以在block内部做removeobject操作,原因应该是和block的特性有关, 在block中会保存变量的值,而不会随变量的值的改变而改变 。

遍历的速率

当数组容量很大的时候,如果只是进行数组遍历的话,使用for-in是最快速的,其次是并发遍历,这个很多人都以为enumerateblock是最快的。

遍历实践tips

数组分组

在开发中,有时需要对数组进行某种情况的分组,比如,一个拥有很多消息模型的数组,我们需要根据消息的创建月份进行分组,那么可以使用下面的方法实现:

nsmutableset *set=[nsmutableset set];
nsarray *array = @[message1, message2, message3, message4, message5, message6, message7];
__block nsarray *tempdataarray = [nsarray arraywitharray:array];
[tempdataarray enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) {
  [set addobject:obj.month];//利用set不重复的特性,得到有多少组,根据数组中消息的月份属性
}];
[set enumerateobjectsusingblock:^(id obj, bool *stop) {//遍历set数组
  nspredicate *predicate = [nspredicate predicatewithformat:@"self.month = %@", obj];//创建谓词筛选器
  nsarray *group = [tempdataarray filteredarrayusingpredicate:predicate];//用数组的过滤方法得到新的数组,在添加的最终的数组
}

倒序遍历

倒序遍历也很常见,可以使用上面的反向遍历来实现。

set排序

这个和emunerate其实没有关系,但是也很实用,我们知道set是无序的,但是有时需要实现有顺序的set,可以使用下面来实现:

//由于set无序,现将set转换成nsarray
nsarray *sortdescriptor = @[[[nssortdescriptor alloc] initwithkey:@"self" ascending:no]];
nsarray *sortsetarray = [set sortedarrayusingdescriptors:sortdescriptor];

其实原理是将set转化成array来实现的。

总结

遍历在我们日常开发中十分常见,根据应用场景,选择合适的遍历方法才是我们需要关系的。这篇文章总结了下遍历的种类和注意事项,希望能帮助到有需要的同学。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网