ios - Realm 模型类的问题

标签 ios swift realm

我正在浏览项目的一些模型类(正在使用 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/

相关文章:

ios - 以编程方式获取 UIImageView 的大小

ios - 带有 xcconfig 链接器标志的 CocoaPods 警告

iOS 应用程序(此类不符合键数据源的键值编码)

swift - Swift : can't get my classes to interact 很新

swift - 如何从 Swift 5 中的另一个类获取数组

linux - 能够连接到对象服务器,但不能打开 Realm

ios - 调用插页式 iAd 只工作一次

swift - Flutter 中的 BLE 支持

swift - Realm - 从迁移 block 中的 Realm 删除对象

swift - 意想不到的大 Realm 文件大小