RealmSwift:如何实现Object?

时间:2016-10-01 12:48:39

标签: ios swift realm

我想使用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,我实际需要做些什么?

1 个答案:

答案 0 :(得分:3)

我的模型声明突然出现两个问题:

  1. 您希望保留的属性需要应用dynamic修饰符,但有一些例外。有关示例,请参阅property declaration cheatsheet

  2. Any initializers you add to your Object subclass must be convenience initializers