Nic*_*ard 6 iphone for-loop objective-c fast-enumeration
在使用快速枚举时,知道何时到达数组中的最后一个对象的最佳方法是什么?有没有比增加int然后将其与数组长度进行比较更好的方法?
Ken*_*ner 15
如果你从一个数组开始,那就去掉最后一个元素(有一个特定的调用)并比较你在枚举器中得到的每个元素.就像是:
id lastEl = [myArray lastObject];
for ( id anEl in myArray )
{
if ( anEl == lastEl )
..... // do the last thing
}
Run Code Online (Sandbox Code Playgroud)
比较成本与计数器相同,但是当您忘记增加计数器时,您不需要增加计数器的成本或总是出现的错误.