我在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
仍然有没有懒惰属性的复合密钥吗?
您找到的解决方案已过时。我会在那儿留下笔记。我建议删除lazy
修饰符并初始化compoundKey
为空字符串。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句