-1

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

Lawris
  • 965
  • 2
  • 9
  • 21
  • For future reference "*But didn't worked*" is never a sufficient description of a problem – did it give you a compile time error? A runtime exception? An unexpected output? Set your house on fire? – Hamish Jun 26 '16 at 08:06
  • Also duplicate of [Swift Dictionary: Get values as array](http://stackoverflow.com/questions/26988167/swift-dictionary-get-values-as-array/26988168#26988168) – Martin R Jun 26 '16 at 08:07

1 Answers1

2
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.

impression7vx
  • 1,728
  • 1
  • 20
  • 50