我想使用RealmSwift,因爲它似乎是一個易於使用的框架,並自行處理大量工作。我閱讀了documentation以瞭解如何使用它。在文檔中寫道,我只需導入SwiftRealm
並讓我的模型繼承自Object
。所以,我對於例如這個簡單的模型:RealmSwift:如何實現對象?
import Foundation
import ObjectMapper
func ==(lhs: ADDRESS, rhs: ADDRESS) -> Bool {
return lhs.hashValue == rhs.hashValue;
}
class VADDRESS : Hashable, Mappable {
private var id: Int64!;
private var street: String!;
private var housenumber: String!;
private var addition: String!;
private var postalcode: String!;
private var location: String!;
private var country: String!;
init() {
self.id = -1;
self.street = "";
self.housenumber = "";
self.addition = "";
self.postalcode = "";
self.location = "";
self.country = "";
}
init(id: Int64, street: String, housenumber: String, addition: String, postalcode: String, location: String, country: String) {
self.id = id;
self.street = street;
self.housenumber = housenumber;
self.addition = addition;
self.postalcode = postalcode;
self.location = location;
self.country = country;
}
required init?(map: Map) {
}
func mapping(map: Map) {
self.id <- map["id"];
self.street <- map["street"];
self.housenumber <- map["housenumber"];
self.addition <- map["addition"];
self.postalcode <- map["postalcode"];
self.location <- map["location"];
self.country <- map["country"];
}
var hashValue: Int {
get {
return "\(self.id),\(self.street),\(self.housenumber),\(self.addition),\(self.postalcode),\(self.location),\(self.country)".hashValue;
}
}
}
如果我現在添加的對象:
import Foundation
import ObjectMapper
func ==(lhs: ADDRESS, rhs: ADDRESS) -> Bool {
return lhs.hashValue == rhs.hashValue;
}
class VADDRESS : Object, Mappable {
private var id: Int64!;
private var street: String!;
private var housenumber: String!;
private var addition: String!;
private var postalcode: String!;
private var location: String!;
private var country: String!;
init() {
self.id = -1;
self.street = "";
self.housenumber = "";
self.addition = "";
self.postalcode = "";
self.location = "";
self.country = "";
}
init(id: Int64, street: String, housenumber: String, addition: String, postalcode: String, location: String, country: String) {
self.id = id;
self.street = street;
self.housenumber = housenumber;
self.addition = addition;
self.postalcode = postalcode;
self.location = location;
self.country = country;
}
required init?(map: Map) {
}
func mapping(map: Map) {
self.id <- map["id"];
self.street <- map["street"];
self.housenumber <- map["housenumber"];
self.addition <- map["addition"];
self.postalcode <- map["postalcode"];
self.location <- map["location"];
self.country <- map["country"];
}
var hashValue: Int {
get {
return "\(self.id),\(self.street),\(self.housenumber),\(self.addition),\(self.postalcode),\(self.location),\(self.country)".hashValue;
}
}
}
我得到了錯誤的錯誤。好的,首先我必須重寫init()
方法,因爲Object
似乎已經有了一個init()
方法。 hashValue
也是如此。所以我這樣做:
import Foundation
import ObjectMapper
func ==(lhs: ADDRESS, rhs: ADDRESS) -> Bool {
return lhs.hashValue == rhs.hashValue;
}
class VADDRESS : Object, Mappable {
private var id: Int64!;
private var street: String!;
private var housenumber: String!;
private var addition: String!;
private var postalcode: String!;
private var location: String!;
private var country: String!;
required init() {
super.init();
self.id = -1;
self.street = "";
self.housenumber = "";
self.addition = "";
self.postalcode = "";
self.location = "";
self.country = "";
}
init(id: Int64, street: String, housenumber: String, addition: String, postalcode: String, location: String, country: String) {
self.id = id;
self.street = street;
self.housenumber = housenumber;
self.addition = addition;
self.postalcode = postalcode;
self.location = location;
self.country = country;
}
required init?(map: Map) {
}
// Here is the ERROR appearing!
func mapping(map: Map) {
self.id <- map["id"];
self.street <- map["street"];
self.housenumber <- map["housenumber"];
self.addition <- map["addition"];
self.postalcode <- map["postalcode"];
self.location <- map["location"];
self.country <- map["country"];
}
override var hashValue: Int {
get {
return "\(self.id),\(self.street),\(self.housenumber),\(self.addition),\(self.postalcode),\(self.location),\(self.country)".hashValue;
}
}
}
但現在還有,我真的不明白一個錯誤(我標誌着在代碼中的位置爲註釋):
'required' initializer 'init(value:schema') must be provided by subclass of 'Object'
好於第一:那沒什麼在文檔中提到。據寫道,我只需要從Object
開始,我就準備好了。
如果我現在添加這個方法我得到另一個錯誤:
'required' initializer 'init(value:schema') must be provided by subclass of 'Object'
第一個錯誤是由於缺少方法:
required init(realm: RLMREalm, schema: RLMObjectSChema) {
fatalError("init(realm:schema:) has not been implemented")
}
而第二個,因爲缺少的:
required init(realm: RLMREalm, schema: RLMObjectSChema) {
fatalError("init(realm:schema:) has not been implemented")
}
所以我一遍又一遍地得到同樣的錯誤,他總是希望我實現一個可以被讀取的方法你在場。
爲了能夠使用RealmSwift,我實際上必須做些什麼?
幾天前我處理了同樣的問題。我認爲他們要求你的對象都有它們屬性的默認值,然後在你需要時使用便捷初始值設定項。這裏有一個關於這個問題的完整主題:https://github.com/realm/realm-cocoa/issues/3185 – kleezy