如何在 Swift 中对 Codables 字典进行编码?
How to encode a dictionary of Codables in Swift?
我在 Swift 中有一个 [String: Codable]
字典,我想将其保存到用户默认值中,但我很难这样做。
我尝试使用
将其转换为Data
try! JSONSerialization.data(withJSONObject: dictionary, options: .init(rawValue: 0))
但这会崩溃 ("Invalid type in JSON write (_SwiftValue)")
我试过使用 JSONEncoder
:
JSONEncoder().encode(dictionary)
但这不会编译 ("Generic parameter T could not be inferred")。
当然我可以手动将我所有的 Codable 转换为 [String: Any] 然后将其写入用户默认值,但由于 Codable 的全部意义在于使解码和编码变得容易,我不太清楚为什么以上两种解决方案都不可行(尤其是第二种)?
示例:
为了重现性,您可以在 Playground 中使用此代码:
import Foundation
struct A: Codable {}
struct B: Codable {}
let dict = [ "a": A(), "b": B() ] as [String : Codable]
let data = try JSONEncoder().encode(dict)
Codable
作为通用约束,并且 Any
不可编码。使用结构而不是字典:
struct A: Codable {
let a = 0
}
struct B: Codable {
let b = "hi"
}
struct C: Codable {
let a: A
let b: B
}
let d = C(a: A(), b: B())
let data = try JSONEncoder().encode(d)
UserDefaults 有办法保存 [String: Any] 字典:
let myDictionary: [String: Any] = ["a": "one", "b": 2]
UserDefaults.standard.set(myDictionary, forKey: "key")
let retrievedDictionary: [String: Any] = UserDefaults.standard.dictionary(forKey: "key")!
print(retrievedDictionary) // prints ["a": one, "b": 2]
但是,如果您的字典是您要保存到 UserDefaults
的对象的 属性,您需要为您的对象实施 Codable
协议。我知道的最简单的方法是使用 JSONSerialization
将字典转换为 Data
对象。以下代码对我有用:
class MyObject: Codable {
let dictionary: [String: Any]
init(dictionary: [String: Any]) {
self.dictionary = dictionary
}
enum CodingKeys: String, CodingKey {
case dictionary
}
public required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
if values.contains(.dictionary), let jsonData = try? values.decode(Data.self, forKey: .dictionary) {
dictionary = (try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any]) ?? [String: Any]()
} else {
dictionary = [String: Any]()
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if !dictionary.isEmpty, let jsonData = try? JSONSerialization.data(withJSONObject: dictionary) {
try container.encode(jsonData, forKey: .dictionary)
}
}
}
要从 UserDefaults
中保存和检索 MyObject
,您可以这样做:
extension UserDefaults {
func set(_ value: MyObject, forKey defaultName: String) {
guard let data = try? PropertyListEncoder().encode(value) else { return }
set(data, forKey: defaultName)
}
func myObject(forKey defaultName: String) -> MyObject? {
guard let data = data(forKey: defaultName) else { return nil }
return try? PropertyListDecoder().decode(MyObject.self, from: data)
}
}
我在 Swift 中有一个 [String: Codable]
字典,我想将其保存到用户默认值中,但我很难这样做。
我尝试使用
将其转换为Data
try! JSONSerialization.data(withJSONObject: dictionary, options: .init(rawValue: 0))
但这会崩溃 ("Invalid type in JSON write (_SwiftValue)")
我试过使用 JSONEncoder
:
JSONEncoder().encode(dictionary)
但这不会编译 ("Generic parameter T could not be inferred")。
当然我可以手动将我所有的 Codable 转换为 [String: Any] 然后将其写入用户默认值,但由于 Codable 的全部意义在于使解码和编码变得容易,我不太清楚为什么以上两种解决方案都不可行(尤其是第二种)?
示例:
为了重现性,您可以在 Playground 中使用此代码:
import Foundation
struct A: Codable {}
struct B: Codable {}
let dict = [ "a": A(), "b": B() ] as [String : Codable]
let data = try JSONEncoder().encode(dict)
Codable
作为通用约束,并且 Any
不可编码。使用结构而不是字典:
struct A: Codable {
let a = 0
}
struct B: Codable {
let b = "hi"
}
struct C: Codable {
let a: A
let b: B
}
let d = C(a: A(), b: B())
let data = try JSONEncoder().encode(d)
UserDefaults 有办法保存 [String: Any] 字典:
let myDictionary: [String: Any] = ["a": "one", "b": 2]
UserDefaults.standard.set(myDictionary, forKey: "key")
let retrievedDictionary: [String: Any] = UserDefaults.standard.dictionary(forKey: "key")!
print(retrievedDictionary) // prints ["a": one, "b": 2]
但是,如果您的字典是您要保存到 UserDefaults
的对象的 属性,您需要为您的对象实施 Codable
协议。我知道的最简单的方法是使用 JSONSerialization
将字典转换为 Data
对象。以下代码对我有用:
class MyObject: Codable {
let dictionary: [String: Any]
init(dictionary: [String: Any]) {
self.dictionary = dictionary
}
enum CodingKeys: String, CodingKey {
case dictionary
}
public required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
if values.contains(.dictionary), let jsonData = try? values.decode(Data.self, forKey: .dictionary) {
dictionary = (try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any]) ?? [String: Any]()
} else {
dictionary = [String: Any]()
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if !dictionary.isEmpty, let jsonData = try? JSONSerialization.data(withJSONObject: dictionary) {
try container.encode(jsonData, forKey: .dictionary)
}
}
}
要从 UserDefaults
中保存和检索 MyObject
,您可以这样做:
extension UserDefaults {
func set(_ value: MyObject, forKey defaultName: String) {
guard let data = try? PropertyListEncoder().encode(value) else { return }
set(data, forKey: defaultName)
}
func myObject(forKey defaultName: String) -> MyObject? {
guard let data = data(forKey: defaultName) else { return nil }
return try? PropertyListDecoder().decode(MyObject.self, from: data)
}
}