我的字典是這樣如何獲得字典的值在數組中迅速
var dict : [String : Array<String>] = ["Fruits" : ["Mango", "Apple", "Banana"],"Flowers" : ["Rose", "Lotus","Jasmine"],"Vegetables" : ["Tomato", "Potato","Chilli"]]
我想值陣列的每個關鍵如何得到它迅速?
我的字典是這樣如何獲得字典的值在數組中迅速
var dict : [String : Array<String>] = ["Fruits" : ["Mango", "Apple", "Banana"],"Flowers" : ["Rose", "Lotus","Jasmine"],"Vegetables" : ["Tomato", "Potato","Chilli"]]
我想值陣列的每個關鍵如何得到它迅速?
編輯: 嘗試這樣的事情,
var dict : [String : Array<String>] = [
"Fruits" : ["Mango", "Apple", "Banana"],
"Flowers" : ["Rose", "Lotus","Jasmine"],
"Vegetables" : ["Tomato", "Potato","Chilli"]
]
var myArray : Array<String> = []
// You can access the dictionary(dict) by the keys(Flowers, Flowers, Vegetables)
// Here I'm appending the array(myArray), by the accessed values.
myArray += dict["Fruits"]!
myArray += dict["Vegetables"]!
print("myArray \(myArray)")
以上是如何。如果你想獲得 詞典(所有值contatenated陣列得到百科的價值在迅速, *只值), 嘗試下面的內容。
print("values array : \(dict.map{$0.value}.flatMap{$0})")
值數組:[ 「玫瑰」, 「蓮花」, 「茉莉花」, 「番茄」, 「土豆」, 「辣椒」, 「芒果」, 「蘋果」, 「香蕉」 ]
嘗試:
var a:Array = dict["Fruits"]! ;
println(a[0])//mango
println(a[1])//apple
試試這個:
for (key, value) in dict {
println("key=\(key), value=\(value)")
}
嘗試獲得價值像下面的代碼
let fruits = dict["Fruits"]
let flowers = dict["Flowers"]
let vegetables = dict["Vegetables"]
for val in dict.values {
print("Value -> \(val)")
}
兩年半的時間,沒有人提到map
?
OK,拋開那Dictionary
有一個方法values
,將你問什麼,你可以這樣做:
let values = dict.map { $0.value }