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

标签 swift realm compound-key

我找到了在 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 不支持惰性属性,在我的例子中我收到了这个错误:

exception NSException * name: "RLMException" - reason: "Lazy managed property 'compoundKey' is not allowed on a Realm Swift object class. Either add the property to the ignored properties list or make it non-lazy." 0x00007f8a05108060

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

最佳答案

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

关于swift - 具有惰性属性的 Realm 中的复合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38010548/

相关文章:

ios - 在两个 ViewController(委托(delegate))之间传递数据 - Swift

ios - 自定义UIButton时不同状态的IBInpectable属性

java - 跨所有线程使用 Realm

ios - 在 UITableviewCell 中使用自动布局

ios - Objective C 项目中的 Swift 框架

swift - 在 Xcode/Cocoapods 项目中更新 swift 版本的正确方法是什么?

android - 使用 Robolectric 和 Mockito 对 Realm + Dagger 2 进行单元测试

MySQL UNIQUE KEY 设计 : compound INT + VARCHAR key vs. 单列 VARCHAR 键

SQLite 表约束 - 在多列上唯一