查找并修改[Int:(String:String)]数组中的元组值

时间:2019-10-02 10:44:00

标签: swift tuples

我尝试根据房间号(103)修改元组的值,以将其从Y设置为N。 我正在尝试使用.firstIndex(where .....但我不知道该写些什么... 任何想法如何查看numeroStanze数组内部以及将元组的特定值从Y修改为N

谢谢


let numeroStanze = [

    100 : ("Single Room", "N"),
    101 : ("Double Room", "N"),
    102 : ("Double Room", "N"),
    103 : ("Double Room", "Y"),
    104 : ("Single Room", "N"),
    105 : ("Single Room", "N")
]


func bookStanza (stanzaNumero: Int) {
    for (numeroStanza) in numeroStanze {
        if let i = numeroStanze.firstIndex(where: {numeroStanza}) {

        }
    }
}

bookStanza(stanzaNumero: 103)

2 个答案:

答案 0 :(得分:2)

您可以尝试

var numeroStanze = [

    100 : ("Single Room", "N"),
    101 : ("Double Room", "N"),
    102 : ("Double Room", "N"),
    103 : ("Double Room", "Y"),
    104 : ("Single Room", "N"),
    105 : ("Single Room", "N")
]

numeroStanze[103]?.1 = "N"

print(numeroStanze)

答案 1 :(得分:0)

首先,我认为使用这样的结构更容易理解:

struct Stanza {
    enum RoomType {
        case single, double
    }

    let roomType: RoomType
    var isBooked: Bool
}

然后您可以使用以下方法修改字典中房间的值:

numeroStanze[103].isBooked = true