Damiano Miazzi
Damiano Miazzi

Reputation: 2325

find and modify a tuple value in array of [Int:(String:String)]

I'm try to modify the value of the tuple base on the room number (103) to set from Y to N. i'm try with .firstIndex(where..... but I don't know what to write... any idea how to look inside the array numeroStanze and modify a specific value of the tuple from Y to N

thanks


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)

Upvotes: 0

Views: 64

Answers (2)

henrik-dmg
henrik-dmg

Reputation: 1493

First of, I think using a struct likes this is way cleaner and easier to reason about:

struct Stanza {
    enum RoomType {
        case single, double
    }

    let roomType: RoomType
    var isBooked: Bool
}

And then you can modify the value of the room in your dictionary by using:

numeroStanze[103].isBooked = true

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100533

You can try

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)

Upvotes: 2

Related Questions