ios 如何将字典转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31845421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert dictionary to array
提问by Nick
I want to convert my dictionary to an array, by showing each [String : Int]
of the dictionary as a string in the array.
我想通过将每个[String : Int]
字典显示为数组中的字符串来将我的字典转换为数组。
For example: ????
例如: ????
var myDict: [String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]
????
I'm aware of myDict.keys.array
and myDict.values.array
, but I want them to show up in an array together. Here's what I mean:
????
???我知道myDict.keys.array
and myDict.values.array
,但我希望它们一起出现在一个数组中。这就是我的意思:???
var myDictConvertedToArray = ["attack 1", "defend 5", "block 12"]
Thanks in advance.
提前致谢。
回答by vacawama
You can use a for loop to iterate through the dictionary key/value pairs to construct your array:
您可以使用 for 循环遍历字典键/值对来构造数组:
var myDict: [String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]
var arr = [String]()
for (key, value) in myDict {
arr.append("\(key) \(value)")
}
Note:Dictionaries are unordered, so the order of your array might not be what you expect.
注意:字典是无序的,所以数组的顺序可能不是你所期望的。
In Swift 2and later, this also can be done with map
:
在Swift 2及更高版本中,这也可以通过以下方式完成map
:
let arr = myDict.map { "\(let arr = myDict.map { "\(let arrayFromDic = Array(dic.values.map{ let dic = ["1":"a", "2":"b","3":"c"]
let ps = Array(dic.values.map{ let array = Array(myDict.keys.map { "\(let array = myDict.keys.array.map { "\(var myDict:[String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]
var dictArray: [String] = []
for (k, v) in myDict {
dictArray.append("\(k) \(v)")
}
) \(myDict[##代码##]!)" }
) \(myDict[##代码##]!)" })
})
print("\(ps)")
for p in ps {
print("\(p)")
}
})
.key) \(##代码##.value)" }
) \()" }
This can also be written as:
这也可以写成:
##代码##which is clearer if not as short.
如果不是那么短,那就更清楚了。
回答by Ali
The general case for creating an array out of ONLY VALUESof a dictionary in Swift 3 is (I assume it also works in older versions of swift):
在 Swift 3 中从字典的ONLY VALUES创建数组的一般情况是(我认为它也适用于旧版本的 swift):
##代码##Example:
例子:
##代码##回答by Antonio
If you like concise code and prefer a functional approach, you can use the map
method executed on the keys collection:
如果你喜欢简洁的代码并且更喜欢函数式方法,你可以使用map
在 keys 集合上执行的方法:
or, as suggested by @vacawama:
或者,正如@vacawama 所建议的那样:
##代码##which is functionally equivalent
这在功能上是等效的
回答by MrHaze
You will have to go through and construct a new array yourself from the keys and the values.
您将不得不通过键和值自己构建一个新数组。
Have a look at ?'s swift array documentation:
You can add a new item to the end of an array by calling the array's append(_:) method:
您可以通过调用数组的 append(_:) 方法将新项添加到数组的末尾:
Try this:
尝试这个:
##代码##Have a look at What's the cleanest way of applying map() to a dictionary in Swift?if you're using Swift 2.0:
看看在 Swift 中将 map() 应用于字典的最干净的方法是什么?如果您使用的是 Swift 2.0: