Plist 与结构不同的字典
Plist with different dictionaries to a struct
我有一个 plist
,它是一个 字典数组 。 (根是数组)。
每个 dic 肯定有 2 个值:name
和 icon
作为 Strings
。
但有些 keys/values,有些 没有 。
我正在尝试将数组读入结构:
struct Config: Decodable {
private enum CodingKeys: String, CodingKey {
case name, icon
}
let name: String
let icon: String
}
func functionsStruct() -> Config {
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
}
我看了一本字典,如何读取一个字典数组,如何设置结构。
有的dic有key,有的没有,如何解决?
如果您想要字典数组,则无需创建自己的 Codable
class。数组和字典已经实现了 Codable
.
就这样:
func functionsStruct() -> [[String: String]] {
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([[String: String]].self, from: data)
}
如果字典包含字符串以外的值,则使用 [[String: Any]]
。
然后您可以像这样访问您的 name
和 icon
:
let dicts = functionsStruct()
print(dicts[0]["name"])
但是,我不明白你为什么非要用字典。我强烈建议您坚持使用 Codable
结构。您可以将可选键设为可选:
struct Config: Decodable {
let name: String
let icon: String
let optionalKey1: String?
let optionalKey2: String?
}
如果 plist 中不存在该键,它们的值将为 nil。
并且您需要解码 Config
的数组,而不是像您所做的那样只解码 Config
:
return try! decoder.decode([Config].self, from: data)
第一个问题:
我看了一本字典,如何读取字典数组,如何设置结构。
func functionsStruct() -> [Config] {
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([Config].self, from: data)
}
就这么简单!!!
我有一个 plist
,它是一个 字典数组 。 (根是数组)。
每个 dic 肯定有 2 个值:name
和 icon
作为 Strings
。
但有些 keys/values,有些 没有 。
我正在尝试将数组读入结构:
struct Config: Decodable {
private enum CodingKeys: String, CodingKey {
case name, icon
}
let name: String
let icon: String
}
func functionsStruct() -> Config {
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)
}
我看了一本字典,如何读取一个字典数组,如何设置结构。
有的dic有key,有的没有,如何解决?
如果您想要字典数组,则无需创建自己的 Codable
class。数组和字典已经实现了 Codable
.
就这样:
func functionsStruct() -> [[String: String]] {
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([[String: String]].self, from: data)
}
如果字典包含字符串以外的值,则使用 [[String: Any]]
。
然后您可以像这样访问您的 name
和 icon
:
let dicts = functionsStruct()
print(dicts[0]["name"])
但是,我不明白你为什么非要用字典。我强烈建议您坚持使用 Codable
结构。您可以将可选键设为可选:
struct Config: Decodable {
let name: String
let icon: String
let optionalKey1: String?
let optionalKey2: String?
}
如果 plist 中不存在该键,它们的值将为 nil。
并且您需要解码 Config
的数组,而不是像您所做的那样只解码 Config
:
return try! decoder.decode([Config].self, from: data)
第一个问题:
我看了一本字典,如何读取字典数组,如何设置结构。
func functionsStruct() -> [Config] {
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([Config].self, from: data)
}
就这么简单!!!