Swift - 如何查找数组中数组中项的索引

时间:2018-01-11 22:00:33

标签: ios arrays swift

我只有10岁,我正在制作一个战争(纸牌游戏)应用程序。在war应用程序中,我不得不将数组放在数组中。如何在这种数组中找到项目的索引? 以下是我会收到错误的示例:

var card_array = [["2_of_clubs", "2_of_diamonds", "2_of_hearts", "2_of_spades"],
                  ["3_of_clubs", "3_of_diamonds", "3_of_hearts", "3_of_spades"],
                  ["4_of_clubs", "4_of_diamonds", "4_of_hearts", "4_of_spades"],
                  ["5_of_clubs", "5_of_diamonds", "5_of_hearts", "5_of_spades"],
                  ["6_of_clubs", "6_of_diamonds", "6_of_hearts", "6_of_spades"],
                  ["7_of_clubs", "7_of_diamonds", "7_of_hearts", "7_of_spades"],
                  ["8_of_clubs", "8_of_diamonds", "8_of_hearts", "8_of_spades"],
                  ["9_of_clubs", "9_of_diamonds", "9_of_hearts", "9_of_spades"],
                  ["10_of_clubs", "10_of_diamonds", "10_of_hearts", "10_of_spades"],
                  ["jack_of_clubs2", "jack_of_diamonds2", "jack_of_hearts2", "jack_of_spades2"],
                  ["queen_of_clubs2", "queen_of_diamonds2", "queen_of_hearts2", "queen_of_spades2"],
                  ["king_of_clubs2", "king_of_diamonds2", "king_of_hearts2", "king_of_spades2"],
                  ["ace_of_clubs", "ace_of_diamonds", "ace_of_hearts", "ace_of_spades"]]
var locationBottom = card_array.index(of: ["4_of_spades"])
var locationTop = card_array.index(of: ["king_of_diamonds2"])
print(locationTop)
print(locationBottom)                                            

3 个答案:

答案 0 :(得分:0)

看起来你想要的是:

var locationBottom = card_array[0].index(of: "4_of_spades")
var locationTop = card_array[0].index(of: "king_of_diamonds2")

相反,您正在寻找一个数组实例,该数组只包含一个包含所有52张卡阵列的数组中的一张卡。

答案 1 :(得分:0)

第一个答案可能是正确的,但它不如以下那么好,因为调用数组的索引而不检查它是否存在会使程序崩溃。 :)

下面是一个示例,首先将嵌套数组展平为一个数组,然后使用if let语法检查作为索引的可选值。

然后它将索引打印到控制台。我还包括了扁平阵列的打印声明,供您查看它的外观。

var card_array = [["2_of_clubs", "2_of_diamonds", "2_of_hearts", "2_of_spades"],
              ["3_of_clubs", "3_of_diamonds", "3_of_hearts", "3_of_spades"],
              ["4_of_clubs", "4_of_diamonds", "4_of_hearts", "4_of_spades"],
              ["5_of_clubs", "5_of_diamonds", "5_of_hearts", "5_of_spades"],
              ["6_of_clubs", "6_of_diamonds", "6_of_hearts", "6_of_spades"],
              ["7_of_clubs", "7_of_diamonds", "7_of_hearts", "7_of_spades"],
              ["8_of_clubs", "8_of_diamonds", "8_of_hearts", "8_of_spades"],
              ["9_of_clubs", "9_of_diamonds", "9_of_hearts", "9_of_spades"],
              ["10_of_clubs", "10_of_diamonds", "10_of_hearts", "10_of_spades"],
              ["jack_of_clubs2", "jack_of_diamonds2", "jack_of_hearts2", "jack_of_spades2"],
              ["queen_of_clubs2", "queen_of_diamonds2", "queen_of_hearts2", "queen_of_spades2"],
              ["king_of_clubs2", "king_of_diamonds2", "king_of_hearts2", "king_of_spades2"],
              ["ace_of_clubs", "ace_of_diamonds", "ace_of_hearts", "ace_of_spades"]]

let flatCardArray = card_array.flatMap({$0})
print(flatCardArray)

if let locationBottom = flatCardArray.index(of: "4_of_spades"), let locationTop = flatCardArray.index(of: "king_of_diamonds2") {
    print("index of locationTop is: ", locationTop)
    print("index of locationBottom is: ", locationBottom)
} else {
    print("oh no, couldn't find indexes!")
}

或者你可以做到

for (number, array) in card_array.enumerated() {
    if let locationBottom = array.index(of: "4_of_spades") {
        print("index of locationBottom is: ", locationBottom, ", in array number: ", number)
    }
    if let locationTop = array.index(of: "king_of_diamonds2") {
        print("index of locationTop is: ", locationTop, ", in array number: ", number)
    }
}

答案 2 :(得分:0)

您正在使用方阵,因此您必须找到两个索引:一个用于行,另一个用于列,这是一个简单的示例:

let elementToFind = "king_of_diamonds2"
var columnIndex:Int?
var rowIndex:Int? = card_array.index(where: { (arrayInside) -> Bool in
    columnIndex = arrayInside.index(where: { (string) -> Bool in
        return string == elementToFind
    })
    return columnIndex != nil
})

print("Row: \(rowIndex!) Column: \(columnIndex!)")