[英]How to implement a for-in loop in Objective-C from the end of collection not from beginning?
[英]Objective-C For-In Loop Get Index
請考慮以下聲明:
for (NSString *string in anArray) {
NSLog(@"%@", string);
}
如何在不使用傳統for
循環的情況下獲取anArray
的string
索引,而不使用anArray
每個對象檢查string
的值?
保證數組按對象順序迭代。 所以:
NSUInteger index = 0;
for(NSString *string in anArray)
{
NSLog(@"%@ is at index %d", string, index);
index++;
}
或者,使用塊枚舉器:
[anArray
enumerateObjectsUsingBlock:
^(NSString *string, NSUInteger index, BOOL *stop)
{
NSLog(@"%@ is at index %d", string, index);
}];
試試這個。
for (NSString *string in anArray)
{
NSUInteger index = [anArray indexOfObject:string];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.