【发布时间】:2016-10-02 22:45:09
【问题描述】:
我想使用RealmSwift,因为它似乎是一个易于使用的框架并且可以自己处理很多工作。我阅读了documentation 以了解如何使用它。在文档中写道,我只需导入SwiftRealm 并让我的模型从Object 继承。所以我有这个简单的模型,例如:
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 inerhit 就可以了。
如果我现在添加这个方法,我会得到另一个错误:
'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?
【问题讨论】:
-
几天前我处理了同样的问题。我认为他们要求您的对象都具有其属性的默认值,然后在需要时使用便利初始化程序。这里有一个关于这个问题的完整主题:github.com/realm/realm-cocoa/issues/3185