I have a dictionary that looks like this:
let ints: [Int: String] = [
0: "0",
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "A",
11: "B",
// etc...
]
I can look up an integer with ints[5]
to get "5"
. How can I look up the integer from the string? I want to do something like ints.keys["5"]
-> 5
.
At the moment, I have just written the dictionary backwards:
let chars: [String: Int] = [
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
"A": 10,
"B": 11,
// etc...
]
I can do chars["5"]
to get 5
, but this is a cumbersome solution since my dictionary is big and want to be able to change it easily if needed.
Clarification
I don't want to programmatically construct the dictionaries, but just keep one hard coded.