领域 Swift 子类化
Realm Swift subclassing
我有两个模型,我正尝试使用 RealmSwift
在 swift 中对其进行子类化
模型 1:用户
@objcMembers class User: Object, Codable {
dynamic var id: Int = 0
dynamic var username: String = ""
dynamic var email: String = ""
dynamic var counts: Counts = Counts(follows: 0, followed_by: 0)
convenience init(id: Int, username: String, email: String, counts: Counts) {
self.init()
self.id = id
self.username = username
self.email = email
self.counts = counts
}
}
class Counts: Codable {
let follows: Int
let followed_by: Int
init(follows: Int, followed_by: Int) {
self.follows = follows
self.followed_by = followed_by
}
}
这里我创建了一个继承codable的模型,因为我使用的是Swift 4种新的解码方式。这个模型工作正常。我可以去添加一个用户并阅读它。所以这样做很好:
let newUser = User(id: 1, username: "user1", email: "user1@gmail.com", counts: Counts(follows: 100, followed_by: 500000000))
RealmService.shared.write(newUser)
现在我正在尝试创建一个名为 Friend 的模型,它有一个继承自 User
的字段
模型 2:朋友
@objcMembers class Friend: Object {
dynamic var user: User = User(id: 0, username: "", email: "", counts: Counts(follows: 0, followed_by: 0))
dynamic var dateAdded: Date = Date(timeIntervalSinceNow: 0)
convenience init(user: User, dateAdded: Date = Date(timeIntervalSinceNow: 0)) {
self.init()
self.user = user
self.dateAdded = dateAdded
}
}
当我这样做并加载我的应用程序时,我得到一个 SIGBART,它突出显示了我的领域初始化:let realm = RealmService.shared.realm
当我删除我的朋友模型时,该应用程序运行正常。
你如何在 Realm 中正确地子类化?
您不能将属性声明为普通 Swift 类型为其他 Realm 子类的属性。您要查找的是 many-to-one relationship.
多对一关系必须声明为可选项,您不能提供默认值
@objcMembers class Friend: Object {
dynamic var user: User?
dynamic var dateAdded: Date = Date()
convenience init(user: User, dateAdded: Date = Date()) {
self.init()
self.user = user
self.dateAdded = dateAdded
}
}
无关,但是当你想创建一个表示当前时间的 Date
对象时,只需调用 Date
的默认空初始化程序,它会为你完成,而且它比Date(timeIntervalSinceNow: 0)
.
我有两个模型,我正尝试使用 RealmSwift
在 swift 中对其进行子类化模型 1:用户
@objcMembers class User: Object, Codable {
dynamic var id: Int = 0
dynamic var username: String = ""
dynamic var email: String = ""
dynamic var counts: Counts = Counts(follows: 0, followed_by: 0)
convenience init(id: Int, username: String, email: String, counts: Counts) {
self.init()
self.id = id
self.username = username
self.email = email
self.counts = counts
}
}
class Counts: Codable {
let follows: Int
let followed_by: Int
init(follows: Int, followed_by: Int) {
self.follows = follows
self.followed_by = followed_by
}
}
这里我创建了一个继承codable的模型,因为我使用的是Swift 4种新的解码方式。这个模型工作正常。我可以去添加一个用户并阅读它。所以这样做很好:
let newUser = User(id: 1, username: "user1", email: "user1@gmail.com", counts: Counts(follows: 100, followed_by: 500000000))
RealmService.shared.write(newUser)
现在我正在尝试创建一个名为 Friend 的模型,它有一个继承自 User
的字段模型 2:朋友
@objcMembers class Friend: Object {
dynamic var user: User = User(id: 0, username: "", email: "", counts: Counts(follows: 0, followed_by: 0))
dynamic var dateAdded: Date = Date(timeIntervalSinceNow: 0)
convenience init(user: User, dateAdded: Date = Date(timeIntervalSinceNow: 0)) {
self.init()
self.user = user
self.dateAdded = dateAdded
}
}
当我这样做并加载我的应用程序时,我得到一个 SIGBART,它突出显示了我的领域初始化:let realm = RealmService.shared.realm
当我删除我的朋友模型时,该应用程序运行正常。
你如何在 Realm 中正确地子类化?
您不能将属性声明为普通 Swift 类型为其他 Realm 子类的属性。您要查找的是 many-to-one relationship.
多对一关系必须声明为可选项,您不能提供默认值
@objcMembers class Friend: Object {
dynamic var user: User?
dynamic var dateAdded: Date = Date()
convenience init(user: User, dateAdded: Date = Date()) {
self.init()
self.user = user
self.dateAdded = dateAdded
}
}
无关,但是当你想创建一个表示当前时间的 Date
对象时,只需调用 Date
的默认空初始化程序,它会为你完成,而且它比Date(timeIntervalSinceNow: 0)
.