Lawris Lawris - 5 months ago 11
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 :

myDictionary.values


and :

myDictionary.keys


But didn't worked...

Any help is greatly appreciated

Answer
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

Edit

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.