Lawris Lawris - 1 year ago 60
Swift Question

Convert dictionary to multiple arrays

How can I convert a dictionary like this :

var myDictionary: Dictionary = ["Data1":"Value1", "Data2":"Value2", "Data3":"Value3"]

Into two arrays like that :

var myArray1 = ["Data1", "Data2", "Data3"]
var myArray2 = ["Value1", "Value2", "Value3"]

I tried to do it with :


and :


But didn't worked...

Any help is greatly appreciated

Answer Source
let someKeys = [String](myDictionary.keys)
//It is only [String] because the keys are Strings

let someValue = [String](myDictionary.values)
//It is only [String] because the values are String

If I had a [Int:String]() then it would be

let someKeys = [Int](someKeys.keys)

because key:Value


I totally forgot, nice job @originaluser2

You can also do

let array = Array(myDictionary.keys)
let array2 = Array(myDictionary.values)

and it will do the hard work for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download