我是新来的迅速和领域.我想要一个复合主键,当我尝试这样的东西:
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" } }
我收到这个错误:
‘主键属性’uuid’不存在于对象’DbLocation’
任何人都可以通过示例来帮助我创建一个复合主键?
解决方法
这应该给你答案:
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)" } }
自定义设置器确保复合键始终被更新,懒惰的关键字确保您第一次访问它,它将从您已经设置的内容派生.
了解更多关于这个话题在this thread这个问题已经被辩论.