Realm中具有延迟属性的复合键 [英] Compound key in Realm with lazy property
问题描述
我发现在Swift中将Realm与复合主键结合使用的绝佳解决方案: https://github.com/realm/realm-cocoa/issues/1192
I found this great solution for using Realm with compound primary key in Swift: 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不支持惰性属性,在我的情况下,我收到此错误:
But I discovered that Realm does not support lazy properties, in my case I receive this error:
exception NSException *名称:"RLMException"-原因:在Realm Swift对象类上不允许使用懒惰的托管属性'compoundKey'.要么将该属性添加到被忽略的属性列表中,要么使其成为非惰性的. 0x00007f8a05108060
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
是否还有可能拥有不带惰性属性的复合密钥?
Is it still possible to have compound key without lazy property?
推荐答案
您找到的解决方案已过时.我会在那儿留下笔记.我建议删除lazy
修饰符并将compoundKey
初始化为空字符串.
The solution you found is outdated. I'll leave a note about that there. I'd suggest removing the lazy
modifier and initializing compoundKey
to an empty string.
这篇关于Realm中具有延迟属性的复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!