Xcode 9.2 ; 斯威夫特 4
我以 plist 的形式手动制作了一个字典。它是一个字典字典,如下图所示:
现在我想从 plist 中获取信息,所以我尝试对其进行解码。但是,我不得不给出所涉及元素的类型。那么,问题是,这个 plist 描述的字典的形式是什么?(例如:字符串数组将被写为 [String])。提前致谢。
更新:这是我正在使用的代码,以及我尝试 [String:[String:Int]] 时的结果。
仅考虑可见子词典时,根词典是[String:Any]
或更具体的。[String[String:Int]]
如果其他字典包含不同的类型,则如果不编写自定义初始化程序[String:Int]
就无法使用。PropertyListDecoder()
更新:
这是一个 q&d 示例,它以通常的方式将字典序列化为属性列表并使用PropertyListDecoder()
.
在操场上试试
let dict = ["diccioNum": ["Num3": 3, "Num4": 4, "Num6": 6, "Num8": 8, "Num9": 9, "Num5": 5, "Num1": 1, "Num7": 7, "Num2": 2]]
do {
let plistData = try PropertyListSerialization.data(fromPropertyList: dict, format: .xml, options: 0)
let plist = try PropertyListDecoder().decode([String: [String:Int]].self, from: plistData)
print(plist)
} catch { print(error) }