【发布时间】:2019-10-11 09:19:16
【问题描述】:
Swift (v 5/5.1) 新手,在 Codables 上遇到了困难...希望从这里的专家那里得到一些建议。
好的,我有一个简单的结构字典,其中键是字符串。我想将字典存储在 UserDefaults 中(然后再检索)。这里有一些非常相似的问题,但主要是解决嵌套结构的问题。
第一次尝试(为简单起见,删除了错误处理):
public struct PriceStruct:Codable {
var myPrice: Double
var myTime: TimeInterval
var selected: Bool
var direction: Int
var myHigh, myLow: Double
enum CodingKeys: String, CodingKey {
case myPrice = "myPrice"
case myTime = "myTime"
case selected = "selected"
case direction = "direction"
case myHigh = "myHigh"
case myLow = "myLow"
}
}
var myPrices: [String: PriceStruct] = [:]
// [fill myPrices with some data...]
func savePrices() {
// error: Attempt to set a non-property-list object
UserDefaults.standard.set(myPrices, forKey: "prices")
}
func loadPrices() {
// obviously this doesn't work either
let myPrices = UserDefaults.standard.data(forKey: "prices")
}
While I assumed from the documentation, that UserDefaults is capable of storing dictionaries, it doesn't - at least for me.
Next thing I tried was using JSONEncoder like this:
// this time with prior JSON encoding
func savePrices() {
// this works
let json = try! JSONEncoder().encode(myPrices)
UserDefaults.standard.set(json as Data, forKey: "prices")
}
func loadPrices() {
// this doesn't work
let json = UserDefaults.standard.data(forKey: "prices")
let decoder = JSONDecoder()
let decoded = try! decoder.decode(PriceStruct.self, from json!)
}
不幸的是,我在尝试从 UserDefaults 加载数据时遇到错误:
Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "myPrice", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"myPrice\", intValue: nil) (\"myPrice\").", underlyingError: nil))
我尝试的其他变体是将编码的 JSON 转换为 UTF8 编码的字符串并存储/检索这个:
func savePrices() {
// this works too
let json = try! JSONEncoder().encode(myPrices)
UserDefaults.standard.set(String(data: json, encoding: .utf8), forKey: "prices")
}
func loadPrices() {
// and this doesn't work either
let json = UserDefaults.standard.string(forKey: "prices")!.data(using: .utf8)
}
因此,从引发的错误来看,CodingKeys 似乎是问题的根源。我尝试使用 NSKeyedArchiver 和 NSKeyedUnarchiver` 进行切换,但没有成功。
我真的想知道是否有一个简单/通用的解决方案可以在 UserDefaults 中保存/加载字典?
感谢您的所有 cmets 和建议。谢谢!
【问题讨论】:
标签: xcode nsuserdefaults codable swift5