0

Have created an Array like this in Swift

var MyArray: [String:[String:[Int]]] = [
    "xx": ["x1": [1, 2, 3], "x2": [4, 5, 6], "x3": [7, 8, 9]],
    "yy": ["y1": [10, 11, 12], "y2": [13, 14, 15], "y3": [16, 17, 18]]


]

And I wonder how to save this to the UserDefaults

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Alex Karapanos
  • 895
  • 2
  • 7
  • 20
  • 1
    This is not a multidimensional array, this is a dictionary containing a dictionary containing an array. – Eric Aya Dec 12 '16 at 14:52
  • Possible duplicate of [Save dictionary in userdefaults in swift 3 with xcode 8](http://stackoverflow.com/questions/37825630/save-dictionary-in-userdefaults-in-swift-3-with-xcode-8) – Daniel Dec 12 '16 at 16:09

1 Answers1

3

As @Eric mentioned in comment this is not a multidimensional array it is nested dictionary, so you can set it using set(_:forKey:) and get dictionary using dictionary(forKey:).

Save dictionary in UserDefaults

let defaults = UserDefaults.standard
defaults.set(MyDict, forKey:"DicKey")

Retrieve dictionary from UserDefaults

if let dic = UserDefaults.standard.dictionary(forKey: "DicKey") as? [String:[String:[Int]]]  {
    print(dic)
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183