我的数组中有一个纬度和经度数组,
NSArray *anArrayOfFloatObjects = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:pinLocation1.latitude],
[NSNumber numberWithDouble:pinLocation1.longitude],
[NSNumber numberWithDouble:pinLocation2.latitude],
[NSNumber numberWithDouble:pinLocation2.longitude],
[NSNumber numberWithDouble:pinLocation3.latitude],
[NSNumber numberWithDouble:pinLocation3.longitude],
nil];
我想做的是使用 switch
语句遍历数组(即 objectAtIndex..)
switch (self.anArrayOfFloatObjects objectAtIndex:) {
case :0
//switch the pin color to red
break;
case :2
//switch pin color to green
break;
default:
break;
}
这显然行不通。有谁知道其他方法吗?
最佳答案
我相信你要找的是
[anArrayOfFloatObjects indexOfObject:number]
也许这段代码会有所帮助:
NSArray *anArrayOfFloatObjects; //Your array
for (NSNumber *number in anArrayOfFloatObjects) {
switch ([anArrayOfFloatObjects indexOfObject:number]) {
case :0
//switch the pin color to red
break;
case :2
//switch pin color to green
break;
default:
break;
}
}
编辑:
在@MikeS 评论之后你可以做这样的事情:
for (int index = 0; index < [anArrayOfFloatObjects count]; index++) {
switch (index) {
case :0
//switch the pin color to red
break;
case :2
//switch pin color to green
break;
default:
break;
}
}
避免调用[anArrayOfFloatObjects indexOfObject:number]
关于ios - 在开关 block 中使用 objectAtIndex 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25628306/