LukeM LukeM - 1 year ago 144
Swift Question

How to update dictionary value in SWIFT

How to
get and
the specific element from the array in the dictionary?

var currency : [String: [Double]] = [
"EUR": [1,1],
"USD": [1.06,1.05],
"CHF": [1.07, 1.06]

I'm working on currency converter and I need to get eg. the second value (buy rate, in this case: "1.05") to make calculations.

Answer Source
let eur_exchangeRate = currency["EUR"]

This will give you the corresponding array of doubles from the dictionary.

let buyRate = eur_exchangeRate![1]

This will give you the second element in the array of doubles (which, I believe is the buy rate you're wanting to access)

currency["EUR"]![1] = 99

This will set your buy rate for EUROS to 99.

Please look at @vadian's comment on your question to get an idea of a cleaner implementation of these sort.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download