ios - 在开关 block 中使用 objectAtIndex 方法

标签 ios objective-c arrays xcode latitude-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..)

   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/

相关文章:

iOS UISearchController 崩溃 : Application tried to present modal view controller on itself

objective-c - Objective-C 中 Nil 与 nil 和 Null 的区别

c++ - Hangman 游戏 - while 循环条件为 "contained in an array"

arrays - 在大小为 N 的数组的每 k 个元素中查找最小和第二小的元素

javascript - Array.push() 和 console.log() 给出了不同的变量

ios - URL 不会加载到 UIWebView 中,但会加载到 safari 浏览器中

ios - 如何加载UITabBarController的所有选项卡?

ios - 将 XML 解析为 NSDictionary

ios - iOS 中的正则表达式替换

objective-c - 如何从 URL 下载图像并将其保存到我的计算机?