如何获得"for(项目中的id项目)"中的数组索引? Objective-C循环? [英] how can I get the array index within an "for (id item in items)" objective-c loop?
本文介绍了如何获得"for(项目中的id项目)"中的数组索引? Objective-C循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Objective-C的"for(项目中的id项目)"循环中获取数组索引?例如,对于NSArray或NSMutableArray.
How can I get the array index within a "for (id item in items)" loop in objective-c? For NSArray or NSMutableArray for example.
例如:
for (id item in items) {
// How to get item's array index here
}
推荐答案
我唯一想到的是:
NSUInteger count = 0;
for (id item in items)
{
//do stuff using count as your index
count++;
}
坏路
或者,您可以使用NSArray
的indexOfObject:
消息来获取索引:
Bad Way
Alternatively, you can use the indexOfObject:
message of a NSArray
to get the index:
NSUInteger index;
for (id item in items)
{
index = [items indexOfObject:item];
//do stuff using index
}
这篇关于如何获得"for(项目中的id项目)"中的数组索引? Objective-C循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文