Mulgard Mulgard - 2 months ago 34
Swift Question

RealmSwift: How to implement the Object?

I wanted to use RealmSwift because it seemed to be an easy to use framework and to handle much work by itself. I read the documentation to find out how to use it. And in the documentation it is written that i simply have to import

SwiftRealm
and to let my model inherit from
Object
. So i have this simple model for example:

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


If i now add the object:

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


I get errors over errors. Ok, so first i have to override the
init()
methode because
Object
seems to already have an
init()
method. same goes for the
hashValue
. So I do it:

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


But now there is an error which i really dont understand ( i marked the position in the code as a comment):

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


Ok at first: Nothing of that is mentioned in the documentation. It was written that i would only have to inerhit from
Object
and i would be ready to go.

If i now add this method i get another error:

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


The first error was due to the missing method:

required init(realm: RLMREalm, schema: RLMObjectSChema) {
fatalError("init(realm:schema:) has not been implemented")
}


And the second because of missing:

required init(realm: RLMREalm, schema: RLMObjectSChema) {
fatalError("init(realm:schema:) has not been implemented")
}


So i get the same error over and over again and he always wants me to implement a method which is already present.

What do i actually have to do to be able to use RealmSwift?

Answer

Two problems jump out at me with your model declaration:

  1. Properties that you wish to persist need to have the dynamic modifier applied to them, with a few exceptions. See the property declaration cheatsheet for examples.

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

Comments