我想使用属性列表解码器来解码字典的二进制plist
制作字典的对象:
struct ZipCode: Codable {
var zipCode: String
var city: String
let state: String
let latitude: String
let longitude: String
let timezone: String
let daylightSavingsFlag: String
let geopoint: String
enum CodingKeys: String, CodingKey {
case zipCode = "Zip"
case city = "City"
case state = "State"
case latitude = "Latitude"
case longitude = "Longitude"
case timezone = "Timezone"
case daylightSavingsFlag = "Daylight savings time flag"
case geopoint = "geopoint"
}
}
包装对象:
struct ZipCodeList: Codable {
var zipCodes: [String:ZipCode]
}
我试图读取它,导致zipCodelist无效:
do {
let path = Bundle.main.path(forResource: "ZipCodes", ofType: "plist")
let binary = FileManager.default.contents(atPath: path!)
let zipCodes = try? PropertyListDecoder().decode(ZipCodeList.self, from: binary!)
print("Hi")
} catch {
}
答案 0 :(得分:1)
您的plist没有元素zipCodes作为根元素,而是解码为
let zipCodes = try? PropertyListDecoder().decode([String: ZipCode].self, from: binary!)