我有这样的字典
var dict : [String : Array<String>] = ["Fruits" : ["Mango", "Apple", "Banana"],"Flowers" : ["Rose", "Lotus","Jasmine"],"Vegetables" : ["Tomato", "Potato","Chilli"]]
我想在每个键的数组中获取值如何在swift中获取它?
答案 0 :(得分:5)
答案 1 :(得分:0)
尝试:
var a:Array = dict["Fruits"]! ;
println(a[0])//mango
println(a[1])//apple
答案 2 :(得分:0)
试试这个:
for (key, value) in dict {
println("key=\(key), value=\(value)")
}
答案 3 :(得分:0)
尝试获取值,如下面的代码
let fruits = dict["Fruits"]
let flowers = dict["Flowers"]
let vegetables = dict["Vegetables"]
答案 4 :(得分:0)
for val in dict.values {
print("Value -> \(val)")
}
答案 5 :(得分:-4)
编辑: 试试这样的事情,
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)")
以上是如何在swift中获取dictionay的值, 如果你想获得所有值的连续数组 字典(*仅限值), 尝试下面的内容。
print("values array : \(dict.map{$0.value}.flatMap{$0})")
值数组:[&#34; Rose&#34;,&#34; Lotus&#34;,&#34; Jasmine&#34;,&#34; Tomato&#34;,&#34; Potato&#34 ;, &#34; Chilli&#34;,&#34; Mango&#34;,&#34; Apple&#34;,&#34; Banana&#34;]