使用 Swift 解码 PropertyList 4 Codable PropertyListDecoder()
Decode PropertyList using Swift 4 Codable PropertyListDecoder()
我正在尝试使用 PropertyListDecoder()
解码 plist,但是当我开始尝试访问密钥时,我收到一条错误消息,指出格式错误。我对自己做错了什么感到茫然。我假设我可以像解码 JSON 文件一样解码 Plist
文件,对吗?我不知道,我对此还是个新手。
//struct for PLists
struct AccessControl: Decodable {
enum AccessControlKeys: String, CodingKey {
case api
}
enum KeySecretKeys: String, CodingKey {
case apiKey = "KEY"
case apiSecret = "SECRET"
}
var KEYS: [KeySecrets]
//custom decoder
init(from decoder: Decoder) throws {
let accessContainer = try decoder.container(keyedBy: AccessControlKeys.self)
let nestedContainer = try accessContainer.nestedContainer(keyedBy: KeySecretKeys.self, forKey: .api)
self.KEYS = try nestedContainer([KeySecrets].self, forKey: .apiKey)
self.KEYS = try nestedContainer.decode([KeySecrets].self, forKey: .apiSecret)
}
}
struct KeySecrets: Decodable {
var apiKey: String
var apiSecret: String
}
func provideAccessKeys(for api: apis = .api, mode: modes = .dev) -> keysForApi? {
switch api {
case .api:
print("Api")
}
switch mode {
case .dev:
print("mode - developers")
case .test:
print("mode - test")
case .prod:
print("mode - production")
}
}
这是我的第一个方法,但它会抛出一个错误
'The data couldn't be read because it was the wrong format'
if let fileURL = Bundle.main.url(forResource: "Accesscontrol", withExtension: "plist") {
do {
let data = try Data.init(contentsOf: fileURL, options: .mappedIfSafe)
let decoder = PropertyListDecoder()
let result = try decoder.decode(AccessControl.self, from: data)
} catch {
print(error.localizedDescription)
}
}
第二种方法,有点放弃了Codable
,仍然无法提取值
guard let fileUrl = Bundle.main.url(forResource: "Accesscontrol", withExtension: "plist") else {return}
let key: String
let secret: String
do {
let data = try Data.init(contentsOf: fileUrl, options: .mappedIfSafe)
let plist = try! PropertyListSerialization.propertyList(from:data, options: [], format: nil) as! [Any]
print(plist)
let dictionary = plist[api.rawValue]
} catch {
print(error.localizedDescription)
}
plist 文件的结构类似于
<plist version="1.0">
<dict>
<key>A_GROUP_OF_KEYS</key>
<array>
<dict>
<key>KEY1</key>
<string>KEY1_STRING</string>
<key>SECRET1_KEY</key>
<string>SECRET1_STRING</string>
</dict>
<dict>
<key>KEY2</key>
<string>KEY2_STRING</string>
<key>SECRET2_KEY</key>
<string>SECRET2_VALUE</string>
</dict>
<dict>
<key>KEY</key>
<string>KEY_STRING</string>
<key>SECRET_KEY</key>
<string>SECRET_VALUE</string>
</dict>
</array>
<key>ANOTHER_GROUP_OF_KEYS</key>
<array>
<dict>
<key>KEY1</key>
<string>KEY1_STRING</string>
<key>SECRET1_KEY</key>
<string>SECRET1_STRING</string>
</dict>
<dict>
<key>KEY2</key>
<string>KEY2_STRING</string>
<key>SECRET2_KEY</key>
<string>SECRET2_VALUE</string>
</dict>
<dict>
<key>KEY</key>
<string>KEY_STRING</string>
<key>SECRET_KEY</key>
<string>SECRET_VALUE</string>
</dict>
</array>
</dict>
</plist>
有什么建议吗?
您的 plist 文件格式错误,因此无法解码。您不应该使用不同的键名来命名每个键,例如 KEY1
、KEY2
、KEY3
等。相反,您应该为键使用一个名称 key
并将值字段中的实际名称。 secret
.
也是如此
这是一个更好的 plist 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>A_GROUP_OF_KEYS</key>
<array>
<dict>
<key>key</key>
<string>KEY1_STRING</string>
<key>secret</key>
<string>SECRET1_STRING</string>
</dict>
<dict>
<key>key</key>
<string>KEY2_STRING</string>
<key>secret</key>
<string>SECRET2_VALUE</string>
</dict>
<dict>
<key>key</key>
<string>KEY3_STRING</string>
<key>secret</key>
<string>SECRET3_VALUE</string>
</dict>
</array>
<key>ANOTHER_GROUP_OF_KEYS</key>
<array>
<dict>
<key>key</key>
<string>KEY1_STRING</string>
<key>secret</key>
<string>SECRET1_STRING</string>
</dict>
<dict>
<key>key</key>
<string>KEY2_STRING</string>
<key>secret</key>
<string>SECRET2_VALUE</string>
</dict>
<dict>
<key>key</key>
<string>KEY3_STRING</string>
<key>secret</key>
<string>SECRET3_VALUE</string>
</dict>
</array>
</dict>
</plist>
解码非常简单:
struct AccessControl: Decodable {
struct Key: Decodable {
var key: String
var secret: String
}
var keyGroup1: [Key]
var keyGroup2: [Key]
enum CodingKeys: String, CodingKey {
case keyGroup1 = "A_GROUP_OF_KEYS"
case keyGroup2 = "ANOTHER_GROUP_OF_KEYS"
}
}
我正在尝试使用 PropertyListDecoder()
解码 plist,但是当我开始尝试访问密钥时,我收到一条错误消息,指出格式错误。我对自己做错了什么感到茫然。我假设我可以像解码 JSON 文件一样解码 Plist
文件,对吗?我不知道,我对此还是个新手。
//struct for PLists
struct AccessControl: Decodable {
enum AccessControlKeys: String, CodingKey {
case api
}
enum KeySecretKeys: String, CodingKey {
case apiKey = "KEY"
case apiSecret = "SECRET"
}
var KEYS: [KeySecrets]
//custom decoder
init(from decoder: Decoder) throws {
let accessContainer = try decoder.container(keyedBy: AccessControlKeys.self)
let nestedContainer = try accessContainer.nestedContainer(keyedBy: KeySecretKeys.self, forKey: .api)
self.KEYS = try nestedContainer([KeySecrets].self, forKey: .apiKey)
self.KEYS = try nestedContainer.decode([KeySecrets].self, forKey: .apiSecret)
}
}
struct KeySecrets: Decodable {
var apiKey: String
var apiSecret: String
}
func provideAccessKeys(for api: apis = .api, mode: modes = .dev) -> keysForApi? {
switch api {
case .api:
print("Api")
}
switch mode {
case .dev:
print("mode - developers")
case .test:
print("mode - test")
case .prod:
print("mode - production")
}
}
这是我的第一个方法,但它会抛出一个错误
'The data couldn't be read because it was the wrong format'
if let fileURL = Bundle.main.url(forResource: "Accesscontrol", withExtension: "plist") {
do {
let data = try Data.init(contentsOf: fileURL, options: .mappedIfSafe)
let decoder = PropertyListDecoder()
let result = try decoder.decode(AccessControl.self, from: data)
} catch {
print(error.localizedDescription)
}
}
第二种方法,有点放弃了Codable
,仍然无法提取值
guard let fileUrl = Bundle.main.url(forResource: "Accesscontrol", withExtension: "plist") else {return}
let key: String
let secret: String
do {
let data = try Data.init(contentsOf: fileUrl, options: .mappedIfSafe)
let plist = try! PropertyListSerialization.propertyList(from:data, options: [], format: nil) as! [Any]
print(plist)
let dictionary = plist[api.rawValue]
} catch {
print(error.localizedDescription)
}
plist 文件的结构类似于
<plist version="1.0">
<dict>
<key>A_GROUP_OF_KEYS</key>
<array>
<dict>
<key>KEY1</key>
<string>KEY1_STRING</string>
<key>SECRET1_KEY</key>
<string>SECRET1_STRING</string>
</dict>
<dict>
<key>KEY2</key>
<string>KEY2_STRING</string>
<key>SECRET2_KEY</key>
<string>SECRET2_VALUE</string>
</dict>
<dict>
<key>KEY</key>
<string>KEY_STRING</string>
<key>SECRET_KEY</key>
<string>SECRET_VALUE</string>
</dict>
</array>
<key>ANOTHER_GROUP_OF_KEYS</key>
<array>
<dict>
<key>KEY1</key>
<string>KEY1_STRING</string>
<key>SECRET1_KEY</key>
<string>SECRET1_STRING</string>
</dict>
<dict>
<key>KEY2</key>
<string>KEY2_STRING</string>
<key>SECRET2_KEY</key>
<string>SECRET2_VALUE</string>
</dict>
<dict>
<key>KEY</key>
<string>KEY_STRING</string>
<key>SECRET_KEY</key>
<string>SECRET_VALUE</string>
</dict>
</array>
</dict>
</plist>
有什么建议吗?
您的 plist 文件格式错误,因此无法解码。您不应该使用不同的键名来命名每个键,例如 KEY1
、KEY2
、KEY3
等。相反,您应该为键使用一个名称 key
并将值字段中的实际名称。 secret
.
这是一个更好的 plist 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>A_GROUP_OF_KEYS</key>
<array>
<dict>
<key>key</key>
<string>KEY1_STRING</string>
<key>secret</key>
<string>SECRET1_STRING</string>
</dict>
<dict>
<key>key</key>
<string>KEY2_STRING</string>
<key>secret</key>
<string>SECRET2_VALUE</string>
</dict>
<dict>
<key>key</key>
<string>KEY3_STRING</string>
<key>secret</key>
<string>SECRET3_VALUE</string>
</dict>
</array>
<key>ANOTHER_GROUP_OF_KEYS</key>
<array>
<dict>
<key>key</key>
<string>KEY1_STRING</string>
<key>secret</key>
<string>SECRET1_STRING</string>
</dict>
<dict>
<key>key</key>
<string>KEY2_STRING</string>
<key>secret</key>
<string>SECRET2_VALUE</string>
</dict>
<dict>
<key>key</key>
<string>KEY3_STRING</string>
<key>secret</key>
<string>SECRET3_VALUE</string>
</dict>
</array>
</dict>
</plist>
解码非常简单:
struct AccessControl: Decodable {
struct Key: Decodable {
var key: String
var secret: String
}
var keyGroup1: [Key]
var keyGroup2: [Key]
enum CodingKeys: String, CodingKey {
case keyGroup1 = "A_GROUP_OF_KEYS"
case keyGroup2 = "ANOTHER_GROUP_OF_KEYS"
}
}