【问题标题】:Compound key in Realm with lazy property具有惰性属性的领域中的复合键
【发布时间】:2016-10-26 21:43:23
【问题描述】:

我找到了在 Swift 中使用 Realm 和复合主键的绝佳解决方案:https://github.com/realm/realm-cocoa/issues/1192

public final class Card: Object {
  public dynamic var id = 0 {
      didSet {
          compoundKey = compoundKeyValue()
      }
  }
  public dynamic var type = "" {
      didSet {
          compoundKey = compoundKeyValue()
      }
  }
  public dynamic lazy var compoundKey: String = self.compoundKeyValue()
  public override static func primaryKey() -> String? {
      return "compoundKey"
  }

  private func compoundKeyValue() -> String {
      return "\(id)-\(type)"
  }
}

但我发现 Realm 不支持惰性属性,在我的情况下我收到此错误:

异常 NSException * 名称:“RLMException” - 原因:“Realm Swift 对象类上不允许使用惰性托管属性 'compoundKey'。要么将该属性添加到忽略的属性列表中,要么使其成为非惰性属性。” 0x00007f8a05108060

是否仍然可以有没有惰性属性的复合键?

【问题讨论】:

    标签: swift realm compound-key


    【解决方案1】:

    您找到的解决方案已过时。我会在那里留下一个便条。我建议删除 lazy 修饰符并将 compoundKey 初始化为空字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 2016-07-01
      • 2018-05-15
      • 1970-01-01
      相关资源
      最近更新 更多