gpt4 book ai didi

ios - Realm 模型类的问题

转载 作者:可可西里 更新时间:2023-11-01 01:56:09 25 4
gpt4 key购买 nike

我正在浏览项目的一些模型类(正在使用 Realm)。这是一节课……

@objcMembers class CommA: Object {
dynamic var id = 0
dynamic var recipientId = "0"
dynamic var name: String?
dynamic var picture: String?
dynamic var unreadMessageCount = 0
dynamic var lastMessage: MyMessage?

override class func primaryKey() -> String? {
return "id"
}
}

这看起来很简单。定义了变量和主键的类..但是还有一个类看起来是这样的……

@objcMembers class CommB: Object, Codable {
dynamic var id = "0"
dynamic var name: String?
dynamic var picture: String?
dynamic var status: String?
dynamic var lastSeen: String?
dynamic var unreadMessageCount = 0
dynamic var lastMessage: MyMessage?

enum CodingKeys: String, CodingKey {
case id = "UsrID"
case name = "UserName"
case picture = "UsrPicture"
case status = "ChatStatus"
}

required init() {
super.init()
}

required init(value: Any, schema: RLMSchema) {
super.init(value: value, schema: schema)
}

required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}

convenience init(id: String, name: String, picture: String, status: String) {
self.init()
self.id = id
self.name = name
self.picture = picture
self.status = status
}

convenience required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let id = try container.decode(String.self, forKey: .id)
let name = try container.decode(String.self, forKey: .name)
let picture = try container.decode(String.self, forKey: .picture)
//let status = try container.decode(String.self, forKey: .status)
self.init(id: id, name: name, picture: picture, status: "status")
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(name, forKey: .name)
try container.encode(picture, forKey: .picture)
try container.encode(status, forKey: .status)
}

override class func primaryKey() -> String? {
return "id"
}
}

这里我不明白的是为什么要使用enumrequired initconvenience required init等……?

最佳答案

事实上,一些初始化器是多余的。您的代码可以缩短为

@objcMembers class CommB: Object, Codable {
dynamic var id = "0"
dynamic var name: String?
dynamic var picture: String?
dynamic var status: String?
dynamic var lastSeen: String?
dynamic var unreadMessageCount = 0
dynamic var lastMessage: MyMessage?

enum CodingKeys: String, CodingKey {
case id = "UsrID"
case name = "UserName"
case picture = "UsrPicture"
case status = "ChatStatus"
}

convenience init(id: String, name: String, picture: String, status: String) {
self.init()
self.id = id
self.name = name
self.picture = picture
self.status = status
}

convenience required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let id = try container.decode(String.self, forKey: .id)
let name = try container.decode(String.self, forKey: .name)
let picture = try container.decode(String.self, forKey: .picture)
//let status = try container.decode(String.self, forKey: .status)
self.init(id: id, name: name, picture: picture, status: "status")
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(name, forKey: .name)
try container.encode(picture, forKey: .picture)
try container.encode(status, forKey: .status)
}

override class func primaryKey() -> String? {
return "id"
}
}

我已经删除了

  required init() {
super.init()
}

required init(value: Any, schema: RLMSchema) {
super.init(value: value, schema: schema)
}

required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}

其他的都很重要。

CommB不仅是一个realm对象,而且还是Codable。并且它的作者想自定义解码/编码行为,以便解码/编码器只解码/编码idnamepicture状态。为此,需要创建一个 CodingKey 枚举,用于存储编码 key 。此外,convenience required init(from decoder: Decoder)func encode(to encoder: Encoder) 需要实现。

convenience init(id: String, name: String, picture: String, status: String) initialiser 在那里是因为 init(from decoder: Decoder) 委托(delegate)

要了解有关 Codable 工作原理的更多信息,请访问 here .

关于ios - Realm 模型类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53274974/

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