I have an dictionary in Swift that looks like this:
我在Swift中有一个字典,如下所示:
[
0: "82",
1: "12",
2: "3",
3: "42"
// Etc.
]
And let's say I want to swap the keys for values 82 and 3, so the new dictionary looks like this:
并且假设我想将键交换为值82和3,因此新词典如下所示:
[
0: "3",
1: "12",
2: "82",
3: "42"
// Etc.
]
How would I do this? (I have found no tips and have no idea how to go about doing this, so I have no code I've tried with this)
我该怎么办? (我没有找到任何提示,也不知道如何去做,所以我没有尝试过这个代码)
EDIT:
编辑:
I just 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
4 个解决方案
#1
5
The basic idea is to create a temporary variable to hold one of the values when you exchange them.
基本思想是创建一个临时变量来在交换时保存其中一个值。
let tmp = dict[0]
dict[0] = dict[2]
dict[2] = tmp
Or you can use the global swap function for this (which does the same thing internally).
或者您可以使用全局交换功能(在内部执行相同的操作)。
var dict = [
0: "82",
1: "12",
2: "3",
3: "42"
]
swap(&dict[0], &dict[2])
#2
6
When you have to swap variables, the simplest way is by using tuples.
当你必须交换变量时,最简单的方法是使用元组。
If you want to swap x
and y
:
如果你想交换x和y:
(x, y) = (y, x)
In your case:
在你的情况下:
(dict[0], dict[2]) = (dict[2], dict[0])
#3
2
You can do some thing like this to simply swap the values,
你可以做这样的事情来简单地交换价值,
var dict = [
0: "82",
1: "12",
2: "3",
3: "42"
// Etc.
]
if let value = dict[key], let existingValue = dict[newKey] {
dict[key] = existingValue
dict[newKey] = value
}
Now, the value of dict is new, with the values you wanted. Or, you could also add category on Dictionary like this,
现在,dict的值是新的,带有你想要的值。或者,你也可以像这样在Dictionary上添加类别,
extension Dictionary {
mutating func swap(key1: Key, key2: Key) {
if let value = self[key1], let existingValue = self[key2] {
self[key1] = existingValue
self[key2] = value
}
}
}
dict.swap(0, key2: 2)
print(dict)
Notice, you dont really need to pass a pointer.
注意,你真的不需要传递指针。
#4
0
var random = ["LAX":"Los Angeles", "JFK":"New York"]
func flip <T, U>(_ dictionary: Dictionary<U, T>) -> Dictionary<T, U> {
let arrayOfValues: [T] = Array(dictionary.values)
let arrayOfKeys: [U] = Array(dictionary.keys)
var newDictionary: [T: U] = [:]
for i in 0...arrayOfValues.count-1 {
newDictionary[arrayOfValues[i]] = arrayOfKeys[i]
}
return newDictionary
}
flip(random) // You will get ["Los Angeles": "LAX", "New York": "JFK"]
#1
5
The basic idea is to create a temporary variable to hold one of the values when you exchange them.
基本思想是创建一个临时变量来在交换时保存其中一个值。
let tmp = dict[0]
dict[0] = dict[2]
dict[2] = tmp
Or you can use the global swap function for this (which does the same thing internally).
或者您可以使用全局交换功能(在内部执行相同的操作)。
var dict = [
0: "82",
1: "12",
2: "3",
3: "42"
]
swap(&dict[0], &dict[2])
#2
6
When you have to swap variables, the simplest way is by using tuples.
当你必须交换变量时,最简单的方法是使用元组。
If you want to swap x
and y
:
如果你想交换x和y:
(x, y) = (y, x)
In your case:
在你的情况下:
(dict[0], dict[2]) = (dict[2], dict[0])
#3
2
You can do some thing like this to simply swap the values,
你可以做这样的事情来简单地交换价值,
var dict = [
0: "82",
1: "12",
2: "3",
3: "42"
// Etc.
]
if let value = dict[key], let existingValue = dict[newKey] {
dict[key] = existingValue
dict[newKey] = value
}
Now, the value of dict is new, with the values you wanted. Or, you could also add category on Dictionary like this,
现在,dict的值是新的,带有你想要的值。或者,你也可以像这样在Dictionary上添加类别,
extension Dictionary {
mutating func swap(key1: Key, key2: Key) {
if let value = self[key1], let existingValue = self[key2] {
self[key1] = existingValue
self[key2] = value
}
}
}
dict.swap(0, key2: 2)
print(dict)
Notice, you dont really need to pass a pointer.
注意,你真的不需要传递指针。
#4
0
var random = ["LAX":"Los Angeles", "JFK":"New York"]
func flip <T, U>(_ dictionary: Dictionary<U, T>) -> Dictionary<T, U> {
let arrayOfValues: [T] = Array(dictionary.values)
let arrayOfKeys: [U] = Array(dictionary.keys)
var newDictionary: [T: U] = [:]
for i in 0...arrayOfValues.count-1 {
newDictionary[arrayOfValues[i]] = arrayOfKeys[i]
}
return newDictionary
}
flip(random) // You will get ["Los Angeles": "LAX", "New York": "JFK"]