2016-10-01 6 views
1

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; 
     } 
    } 
} 

私はエラーを超えるエラーが発生します。さて、Objectは既にinit()メソッドを持っているようですので、最初にinit()メソッドをオーバーライドする必要があります。 hashValueでも同じです。だから私はそれを行う:最初の

'required' initializer 'init(value:schema') must be provided by subclass of 'Object' 

OK::

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; 
     } 
    } 
} 

しかし、今私は本当に理解しないエラーが発生している(私はコメントとして、コード内の位置をマークしていない)ことを何もドキュメントに記載されています。私は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") 
} 

私は何度も何度も同じエラーを繰り返しています。彼はいつも、私が大事にしている方法y存在する。

RealmSwiftを使用するには実際に何が必要ですか?

+0

私は数日前に同じ問題を扱った。私は彼らのオブジェクトがすべてプロパティのデフォルト値を持っている必要があると思うし、必要なときに便利なイニシャライザを使います。この問題については、こちらのスレッドがあります:https://github.com/realm/realm-cocoa/issues/3185 – kleezy

答えて

関連する問題