作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有这个 NSArray:
NSArray* temp=[[NSArray alloc]
initWithObjects:@"one",@"five",@"two",nil];
for(NSString* obj in temp){
NSLog(@"current item:%@ Next item is:%@",obj, ***blank***);
}
什么需要替换空白
?我需要知道即将到来的对象吗?
最佳答案
这仅在您的对象是唯一的(即数组中没有相同的对象)时有效:
id nxt = nil;
int nxtIdx = [temp indexOfObject:idx] + 1;
if (nxtIdx < temp.count) {
nxt = [temp objectAtIndex:nxtIdx];
}
NSLog(@"current item:%@ Next item is:%@", obj, nxt);
但在我看来,这是一个 hack。为什么不使用带有对象索引的普通 for 循环:
for (int i = 0; i < temp.count; i++) {
id obj = [temp objectAtIndex:i];
id next = (i + 1 < temp.count) ? [temp objectAtIndex:i + 1] : nil;
}
或者(推荐)用 block 枚举它
[temp enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id next = nil;
if (idx + 1 < temp.count) {
next = [temp objectAtIndex:idx + 1];
}
}];
关于ios - 有没有办法在 for..in 循环中检查 NSArray 中的下一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14611425/
我是一名优秀的程序员,十分优秀!