poojathorat poojathorat - 10 months ago 50
Swift Question

How to get values of dictionary in array in swift

I have dictionary like this

var dict : [String : Array<String>] = ["Fruits" : ["Mango", "Apple", "Banana"],"Flowers" : ["Rose", "Lotus","Jasmine"],"Vegetables" : ["Tomato", "Potato","Chilli"]]

I want to get values in array for each key How to get it in swift?

Answer Source

EDIT: Try Something like this,

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["Flowers"]!
myArray += dict["Vegetables"]!

print("myArray \(myArray)")