复合主键 realm/swift
composite primary key realm/swift
我是 swift 领域的新手。我想制作一个复合主键,当我尝试这样的事情时:
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'
上不存在
谁能帮我举例说明如何创建复合主键?
1.0.1+
领域:
class DbLocation: Object{
dynamic var id = 0
dynamic var tourId = 0
dynamic var compoundKey = ""
override static func primaryKey() -> String? {
return "compoundKey"
}
func setup(id: Int, tourId: Int){
self.id = id
self.tourId = tourId
self.compoundKey = compoundKeyValue()
}
func compoundKeyValue() -> String {
return "\(id)\(tourId)"
}
}
用法示例:
let location = DbLocation()
location.setup(id: 0, tourId: 1)
print(location.compoundKey) // "01"
当然,您可以尝试在 id
和 tourId
上使用各种 didSet
侦听器,以确保每次更改值时都能正确重写 compoundKey。
pre-1.0.1
领域:
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)"
}
}
自定义设置器确保 compoundKey 始终更新,惰性关键字确保您第一次访问它时,它将派生自您已经设置的内容。
在讨论此问题的 this thread 中找到有关此主题的更多信息。
简单地创建一个新的 属性,其值设置为您希望作为复合主键的其他感兴趣的属性。
class DbLocation: Object {
dynamic var id = 0
dynamic var tourId = 0
dynamic var compoundKey: String? = ""
override static func primaryKey() -> String? {
return "compoundKey"
}
}
let location = DbLocation()
location.tourId = 1
location.id = 5
location.compoundKey = "\(id) \(tourId)"
对于 Swift 和 Realm 的最新版本,我会做这样的事情。
dynamic private var compoundKey: String = ""
required convenience init?(map: Map) {
self.init()
if let firstValue = map.JSON["firstValue"] as? String,
let secondValue = map.JSON["secondValue"] as? Int {
compoundKey = firstValue + "|someStringToDistinguish|" + "\(secondValue)"
}
}
我是 swift 领域的新手。我想制作一个复合主键,当我尝试这样的事情时:
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'
上不存在谁能帮我举例说明如何创建复合主键?
1.0.1+
领域:
class DbLocation: Object{
dynamic var id = 0
dynamic var tourId = 0
dynamic var compoundKey = ""
override static func primaryKey() -> String? {
return "compoundKey"
}
func setup(id: Int, tourId: Int){
self.id = id
self.tourId = tourId
self.compoundKey = compoundKeyValue()
}
func compoundKeyValue() -> String {
return "\(id)\(tourId)"
}
}
用法示例:
let location = DbLocation()
location.setup(id: 0, tourId: 1)
print(location.compoundKey) // "01"
当然,您可以尝试在 id
和 tourId
上使用各种 didSet
侦听器,以确保每次更改值时都能正确重写 compoundKey。
pre-1.0.1
领域:
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)"
}
}
自定义设置器确保 compoundKey 始终更新,惰性关键字确保您第一次访问它时,它将派生自您已经设置的内容。
在讨论此问题的 this thread 中找到有关此主题的更多信息。
简单地创建一个新的 属性,其值设置为您希望作为复合主键的其他感兴趣的属性。
class DbLocation: Object {
dynamic var id = 0
dynamic var tourId = 0
dynamic var compoundKey: String? = ""
override static func primaryKey() -> String? {
return "compoundKey"
}
}
let location = DbLocation()
location.tourId = 1
location.id = 5
location.compoundKey = "\(id) \(tourId)"
对于 Swift 和 Realm 的最新版本,我会做这样的事情。
dynamic private var compoundKey: String = ""
required convenience init?(map: Map) {
self.init()
if let firstValue = map.JSON["firstValue"] as? String,
let secondValue = map.JSON["secondValue"] as? Int {
compoundKey = firstValue + "|someStringToDistinguish|" + "\(secondValue)"
}
}