欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS中遍历的方法总结

程序员文章站 2024-02-08 22:28:16
在ios开发中,可以使用多种方法进行元素遍历,具体有一下几种: 经典for循环 nsarray *iosarray = @[@"a", @"b", @"c",...

在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来实现的。

总结

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

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