我是iOS开发的新手,我只是按照互联网上的教程进行操作
我在实现领域之前的数据模型是这样的
import Foundation
import FirebaseFirestore
class CityKM {
var name : String
var coordinate : GeoPoint
init (name: String , coordinate: GeoPoint ) {
self.name = name
self.coordinate = coordinate
}
init (dictionary: [String:Any]) {
// this init will be used if we get data from firebase to construct a city object
name = dictionary["name"] as! String
coordinate = dictionary["coordinate"] as! GeoPoint
}
}
在实现Import RealmSwift之后,我的类应该通过从领域中继承Object
import Foundation
import FirebaseFirestore
import RealmSwift
class CityKM : Object {
@objc dynamic var name : String = ""
@objc dynamic var var coordinate : GeoPoint = Geopoint(latitude: 6.01212, Longitude: -101.212)
init (name: String , coordinate: GeoPoint ) {
self.name = name
self.coordinate = coordinate
}
init (dictionary: [String:Any]) {
// this init will be used if we get data from firebase to construct a city object
name = dictionary["name"] as! String
coordinate = dictionary["coordinate"] as! GeoPoint
}
}
但我不知道为什么它总是会给出一个错误,说我的CityKM类应该需要初始化程序
必需'初始化'init()'必须由子类提供 '对象'
如果我修复了错误并按照xcode中的修复指令,错误仍然存在,我不知道如何解决这个问题,如下图所示
出了什么问题,我该怎么做?
答案 0 :(得分:2)
尝试声明 CityKM课程,就像这样
您可以在此处找到使用convenience
Initialization Apple
class CityKM : Object {
@objc dynamic var name : String = ""
@objc dynamic var let coordinate : GeoPoint = Geopoint(latitude: 6.01212, Longitude: -101.212)
convenience init (name: String , coordinate: GeoPoint ) {
self.init()
self.name = name
self.coordinate = coordinate
}
convenience init (dictionary: [String:Any]) {
self.init()
// this init will be used if we get data from firebase to construct a city object
name = dictionary["name"] as! String
coordinate = dictionary["coordinate"] as! GeoPoint
}
}