在常规for循环中,我可以i用作for循环中的计数器.我怎么知道上面的计数?
for(int i=0;i<[someArray count];i++)
{
bla = [arrayExample objectAtIndex:i];
}
for(id someObject in someArray)
{
bla = [arrayExample objectAtIndex:??];
}
Run Code Online (Sandbox Code Playgroud)
您可以使用普通的for循环,或者只需将计数器添加到当前的快速枚举中.
这仍然具有快速枚举的优势,同时还包括您当前使用的索引.
int index = 0;
for (id element in someArray) {
//do stuff
++index;
}
Run Code Online (Sandbox Code Playgroud)
更好的是使用快速枚举块方法......
[someArray enumerateWithUsingBlock:^(id element, NSUInteger idx, BOOL stop) {
// you can do stuff in here.
// you also get the current index for free
// idx is the index of the current object in the array
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2784 次 |
| 最近记录: |