我得到一个没有初始化器错误的类。这不是我上课的方式吗?这用于领域对象数据库,即我要存储的属性。我是快速入门的新手,但我认为这是可以做到的。如果我取出init()函数并仅分配它可以工作的所有值,但是,诸如justTook之类的属性以及我不希望使用的所有属性都以一个值开头,则需要在程序后面进行设置。
这是我的代码:
class MedInfo: Object {
// Info To Be Stored
@objc dynamic var keyID = UUID().uuidString
@objc dynamic var medName: String
@objc dynamic var medDose: String
@objc dynamic var currentDate: Date
@objc dynamic var timeTook: Date
@objc dynamic var lastTook: Date
@objc dynamic var durationOfTime: Date
@objc dynamic var doctorName: String
@objc dynamic var rxDate: Date
@objc dynamic var medInfo: String
@objc dynamic var pictureOfMed: UIImage
override static func primaryKey() -> String? {
return "keyID"
}
convenience init(medName: String, medDose: String, currentDate: Date, timeTook: Date, lastTook: Date, durationOfTime: Date, doctorName: String,rxDate: Date, medInfo: String, pictureOfMed: UIImage) {
self.medName = medName
self.medDose = medDose
self.currentDate = currentDate
self.timeTook = timeTook
self.lastTook = lastTook
self.durationOfTime = durationOfTime
self.doctorName = doctorName
self.rxDate = rxDate
self.medInfo = medInfo
self.pictureOfMed = pictureOfMed
}
答案 0 :(得分:1)
您需要为所有参数设置默认值,如文档中模糊描述的那样。来自here:
在Swift中使用Realm时,Swift.reflect(_ :)函数用于 确定有关模型的信息,这需要调用 init()成功。 这意味着所有非可选属性都必须具有 默认值。
所有示例都说明了这一点,因此请遵循它们。因此,应该声明您的课程:
class MedInfo: Object {
// Info To Be Stored
@objc dynamic var keyID = UUID().uuidString
@objc dynamic var medName: String = ""
@objc dynamic var medDose: String = ""
@objc dynamic var currentDate: Date = Date()
@objc dynamic var timeTook: Date = Date()
@objc dynamic var lastTook: Date = Date()
@objc dynamic var durationOfTime: Date = Date()
@objc dynamic var doctorName: String = ""
@objc dynamic var rxDate: Date = Date()
@objc dynamic var medInfo: String = ""
// UIImage not supported, you'll need to store a URL/filename or something else
@objc dynamic var pictureOfMed: UIImage
答案 1 :(得分:0)
您必须调用init来创建实例。
convenience init(medName: String, medDose: String, currentDate: Date, timeTook: Date, lastTook: Date, durationOfTime: Date, doctorName: String,rxDate: Date, medInfo: String, pictureOfMed: UIImage) {
self.init()
self.medName = medName
self.medDose = medDose
self.currentDate = currentDate
self.timeTook = timeTook
self.lastTook = lastTook
self.durationOfTime = durationOfTime
self.doctorName = doctorName
self.rxDate = rxDate
self.medInfo = medInfo
self.pictureOfMed = pictureOfMed
}
答案 2 :(得分:0)
正如@ces在Realm文档中已正确引用:“ 这意味着所有非可选属性都必须具有默认值。”
这就是为什么我认为创建Realm对象的最佳方法是将变量声明为可选变量。
这样,您将不必调用init。这是我创建自己的Realm对象的方式,也是大多数人创建它们的方式。
所以您的对象看起来像这样:
class MedInfo: Object {
// Info To Be Stored
@objc dynamic var keyID = UUID().uuidString
@objc dynamic var medName: String?
@objc dynamic var medDose: String?
@objc dynamic var currentDate: Date?
@objc dynamic var timeTook: Date?
@objc dynamic var lastTook: Date?
@objc dynamic var durationOfTime: Date?
@objc dynamic var doctorName: String?
@objc dynamic var rxDate: Date?
@objc dynamic var medInfo: String?
@objc dynamic var pictureOfMed: UIImage?
override static func primaryKey() -> String? {
return "keyID"
}