gpt4 book ai didi

swift - 如何编码 Realm 的 List<> 类型

转载 作者:可可西里 更新时间:2023-11-01 00:33:21 24 4
gpt4 key购买 nike

我正在尝试将我的 Realm 数据库编码为 JSON。除 List<> 外一切正常编码。所以我的问题是,你会如何编码 List<> ?因为List不符合Encodable neighter Decodable 协议(protocol)。

现在我正在这样做:

@objcMembers class User: Object, Codable{
dynamic var name: String = ""
let dogs = List<Dog>()


private enum UserCodingKeys: String, CodingKey {
case name
case dogs
}

convenience init(name: String) {
self.init()
self.name = name
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: UserCodingKeys.self)
try container.encode(name, forKey: .name)

}



@objcMembers class Dog: Object, Codable{
dynamic var name: String = ""
dynamic var user: User? = nil

private enum DogCodingKeys: String, CodingKey {
case name
}

convenience init(name: String) {
self.init()
name = name
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: DogCodingKeys.self)
try container.encode(name, forKey: .name)

}
}

我正在尝试这样做:

var json: Any?
let user = RealmService.shared.getUsers()
var usersArray = [User]()
for user in users{
usersArray.append(user)
}

let jsonEncoder = JSONEncoder()
let jsonDecoder = JSONDecoder()
let encodedJson = try? jsonEncoder.encode(portfoliosArray)


if let data = encodedJson {
json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
if let json = json {
print(String(describing: json))
}
}

所以问题是我如何能够对 List<Dog>? 进行编码

最佳答案

创建具有类型属性的 Realm 对象模型类 List符合 Encodable , 你可以简单地转换 ListArrayencode(to:)方法,可以自动编码。

extension User: Encodable {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.username, forKey: .username)
let dogsArray = Array(self.dogs)
try container.encode(dogsArray, forKey: .dogs)
}
}

我使用的测试类(与您问题中的测试类略有不同,但我手头已经有了这些,无论变量名称如何,所讨论的方法几乎相同):

class Dog: Object,Codable {
@objc dynamic var id:Int = 0
@objc dynamic var name:String = ""
}

class User: Object, Decodable {
@objc dynamic var id:Int = 0
@objc dynamic var username:String = ""
@objc dynamic var email:String = ""
let dogs = List<Dog>()

private enum CodingKeys: String, CodingKey {
case id, username, email, dogs
}

required convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
username = try container.decode(String.self, forKey: .username)
email = try container.decode(String.self, forKey: .email)
let dogsArray = try container.decode([Dog].self, forKey: .dogs)
dogs.append(objectsIn: dogsArray)
}
}

测试编码/解码:

let userJSON = """
{
"id":1,
"username":"John",
"email":"example@ex.com",
"dogs":[
{"id":2,"name":"King"},
{"id":3,"name":"Kong"}
]
}
"""

do {
let decodedUser = try JSONDecoder().decode(User.self, from: userJSON.data(using: .utf8)!)
let encodedUser = try JSONEncoder().encode(decodedUser)
print(String(data: encodedUser, encoding: .utf8)!)
} catch {
print(error)
}

输出:

{"username":"John","dogs":[{"id":2,"name":"King"},{"id":3,"name":"Kong"}]}

关于swift - 如何编码 Realm 的 List<> 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com