【发布时间】: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