简体   繁体   English

循环的多个条件?

[英]Multiple conditions for loop?

I have this code now: This is iOS我现在有这个代码:这是 iOS

   for(NSDictionary * dict in myMutableArray)
{
    NSString * str = [dict objectForKey:@"MyStringKey"];
    [sheet addButtonWithTitle:str];
}

I need to also have a for loop like this:我还需要有这样的 for 循环:

NSInteger *count = [myMutableArray count];
for (i = 0; i < count; i++) {
    arrayVar = [myMutableArray objectAtIndex:i];
}

I need to kind of combine these.我需要把这些结合起来。 I am inserting a segment into a UISegmentedControl while getting the name from a for loop which is looping through the array getting nsdictionary which is retrieving a nsstring that I will put into the uisegmentedcontrol.我正在向 UISegmentedControl 中插入一个段,同时从一个 for 循环中获取名称,该循环通过数组获取 nsdictionary,它正在检索我将放入 uisegmentedcontrol 的 nsstring。

So bottom line is I need to merge these for loops so I get the name like I do from the first for loop while still getting the objectatindex like it is in the second for loop.所以底线是我需要合并这些 for 循环,所以我从第一个 for 循环中获得名称,同时仍然像在第二个 for 循环中一样获得 objectatindex。

How would I do this?我该怎么做? Thanks!谢谢!

The second version you have will work just fine.您拥有的第二个版本可以正常工作。 There is no way to access the index in the first version.在第一个版本中无法访问索引。 There is a newer syntax available if you like, that uses blocks:如果您愿意,可以使用更新的语法,它使用块:

  NSArray *array;
  [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSDictionary *dict = (NSDictionary *)obj;
    // whatever else
  }];

The main benefit of blocks is they can use multiple cores in your device more effectively.块的主要好处是它们可以更有效地使用设备中的多个内核。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM