[
0: "82" ,
1: "12",
2: "3",
3: "42"
// Etc.
]
And suppose I want to swap the keys to the values 82 and 3, so the new dictionary looks like this:
[
0: "3",
1 : "12",
2: "82",
3: "42"
// Etc.
]
What should I do? (I didn’t find any hints and don’t know how to do it, so I haven’t tried this code)
Edit:
I did this:
var first_key = 0
var second_key = 2
var first_value = dict[first_key]!
var second_value = dict[second_key]!
dict[first_key ] = second_value
dict[second_key] = first_value
let tmp = dict[0]
dict[0] = dict[2]
dict[2] = tmp
Or you can use the global swap function (do the same operation internally).
var dict = [< br /> 0: "82",
1: "12",
2: "3",
3: "42"
]
swap(&dict[0], &dict[2])
I have a dictionary in Swift, as shown below:
[
0: "82",
1: "12",
2: "3",
3: "42"
// Etc.
]
And suppose I want to swap the keys to the values 82 and 3, so the new dictionary looks like this:
[
0: "3",
1: "12",
2: "82",
3: "42"
// Etc.< br />]
What should I do? (I didn’t find any hints and don’t know how to do it, so I haven’t tried this code)
Edit:
I did this:
var first_key = 0
var second_key = 2
var first_value = dict[first_key]!
var second_value = dict[second_key]!
dict[first_key ] = second_value
dict[second_key] = first_value
The basic idea is to create a temporary variable to save one of the values when swapping them. < p>
let tmp = dict[0]
dict[0] = dict[2]
dict[2] = tmp
Or you can use the global swap function (do the same operation internally).
var dict = [
0: "82",
1: "12",
2: "3",
3: "42"
]
swap(&dict[0], &dict[2] )