Ios 在开关块中使用objectAtIndex方法
在我的数组中有一个晶格和经度数组Ios 在开关块中使用objectAtIndex方法,ios,objective-c,arrays,xcode,latitude-longitude,Ios,Objective C,Arrays,Xcode,Latitude Longitude,在我的数组中有一个晶格和经度数组 NSArray *anArrayOfFloatObjects = [NSArray arrayWithObjects: [NSNumber numberWithDouble:pinLocation1.latitude], [NSNumber numberWithDouble:pinLocation1.longitude],
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..)
这显然行不通。有人知道其他方法吗?我相信你要找的是
[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;
}
}
for(int index=0;index<[anArrayOfFloatObjects count];index++){
开关(索引){
案例:0
//将引脚颜色切换为红色
打破
案例:2
//将引脚颜色切换为绿色
打破
违约:
打破
}
}
避免调用
[anArrayOfFloatObjects indexOfObject:number]
我相信您需要的是
[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;
}
}
for(int index=0;index<[anArrayOfFloatObjects count];index++){
开关(索引){
案例:0
//将引脚颜色切换为红色
打破
案例:2
//将引脚颜色切换为绿色
打破
违约:
打破
}
}
避免调用[anArrayOfFloatObjects indexOfObject:number]
尝试快速枚举
for(id item in anArrayOfFloatObjects){
NSLog(@"%@", item);
}
尝试快速枚举
for(id item in anArrayOfFloatObjects){
NSLog(@"%@", item);
}
您还可以查看NSArray的
enumerateObjectsUsingBlock
[yourArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Index: %ld - Object: %@", idx, obj);
}];
您还可以查看NSArray的
enumerateObjectsUsingBlock
[yourArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Index: %ld - Object: %@", idx, obj);
}];
这会添加一个不必要的方法调用。标准的
for
循环更好:for(int index=0;index
。那么您就不需要调用indexOfObject:
,因为您已经有了索引。您可以使用以下命令获取number
:NSNumber*number=anArrayOfFloatObjects[index]代码>你是对的。我专注于展示indexOfObject方法,但忘记了这一点。我将编辑我的答案,谢谢@mikest,它添加了一个不必要的方法调用。标准的for
循环更好:for(int index=0;index
。那么您就不需要调用indexOfObject:
,因为您已经有了索引。您可以使用以下命令获取number
:NSNumber*number=anArrayOfFloatObjects[index]代码>你是对的。我专注于展示indexOfObject方法,但忘记了这一点。我将编辑我的答案,谢谢@mikes与您的问题没有直接关系,但您的数组结构看起来有点奇怪-您可能会从数组中的连续元素中提取每个管脚的纬度和经度。我会将每个管脚的lat和long放入一个数组中,然后将这些数组放入一个数组中。谢谢。说得好。这只是为了我的作业,与你的问题没有直接关系,但是你的数组结构看起来有点奇怪-你可能会从数组中的连续元素中提取每个pin的纬度和经度。我会将每个管脚的lat和long放入一个数组中,然后将这些数组放入一个数组中。谢谢。说得好。这只是为了我的家庭作业