具有惰性属性的Realm中的复合键

Mar*_*rco 3 compound-key realm swift

我发现在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)"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

exception NSException*name:"RLMException" - reason:"在Realm Swift对象类上不允许使用惰性托管属性'compoundKey'.要么将属性添加到忽略的属性列表中,要么将其设置为非惰性." 0x00007f8a05108060

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

mar*_*ius 7

您找到的解决方案已过时.我会在那里留言.我建议删除lazy修饰符并初始化compoundKey为空字符串.