如何将多维数组保存到 UserDefaults [英] How to save a multidimensional array to UserDefaults
本文介绍了如何将多维数组保存到 UserDefaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Swift 中创建了一个这样的 Array
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]]
]
我想知道如何将其保存到 UserDefaults
And I wonder how to save this to the UserDefaults
推荐答案
正如@Eric 在评论中提到的,这不是多维数组,它是嵌套字典,因此您可以使用 set(_:forKey:)
并使用 dictionary(forKey:)
.
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:)
.
在UserDefaults
let defaults = UserDefaults.standard
defaults.set(MyDict, forKey:"DicKey")
从UserDefaults
if let dic = UserDefaults.standard.dictionary(forKey: "DicKey") as? [String:[String:[Int]]] {
print(dic)
}
这篇关于如何将多维数组保存到 UserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文