问题描述
我是快速和领域的新手。我想制作复合主键,当我尝试这样的事情时:
I'm new to swift and realm. I want to make a composite primary key and when I'm trying something like this :
class DbLocation : Object {
dynamic var id = 0
dynamic var tourId = 0
dynamic var uuid : String {
return "\(id)\(tourId)"
}
override static func primaryKey() -> String? {
return "uuid"
}
}
我'我收到此错误:
'对象'DbLocation'上不存在主键属性'uuid'
I'm getting this error :'Primary key property 'uuid' does not exist on object 'DbLocation'
任何人都可以帮我解决如何创建的示例复合主键?
Anyone can help me out with an example how to create a composite primary key ?
推荐答案
这应该给你答案:
class DbLocation: Object {
dynamic var id = 0
dynamic var tourId = 0
func setCompoundID(id: Int) {
self.id = id
compoundKey = compoundKeyValue()
}
func setCompoundTourId(tourId: Int) {
self.tourId = tourId
compoundKey = compoundKeyValue()
}
dynamic lazy var compoundKey: String = self.compoundKeyValue()
override static func primaryKey() -> String? {
return "compoundKey"
}
func compoundKeyValue() -> String {
return "\(id)\(tourId)"
}
}
自定义设置器确保复合键始终更新,懒惰关键字确保第一次访问它时,它将从您已设置的内容中派生出来。
The custom setters make sure, that the compoundKey is always updated, the lazy key word makes sure that the first time you access it, it will be derived from what you've already set.
在中了解有关此主题的更多信息这个问题一直存在争议。
Find out more on this topic in this thread where this issue has been debated.
这篇关于复合主键领域/ swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!