thoma
thoma

Reputation: 583

Swift 3.0.2 and Realm - Compile Errors

I am bumping into an issue while trying to use Realm in a Swift 3.0.2 iOS project. Starting simple, I would like to apply it to a class named Genre:

import Foundation
import RealmSwift

class Genre: Object {
    dynamic var id: Int
    dynamic var name: String

    init?(id: Int, name: String) {
        self.id = id
        self.name = name
        super.init()
    }

}

Looks quite simple, right? Despite this, I am getting the following compile error:

Genre.swift:23:1: 'required' initializer 'init()' must be provided by subclass of 'Object'

Genre.swift:23:1: 'required' initializer 'init(realm:schema:)' must be provided by subclass of 'Object'

Genre.swift:23:1: 'required' initializer 'init(value:schema:)' must be provided by subclass of 'Object'

Any got a hint on how I could solve this? From what I have seen online, it shouldn't be necessary for me to implement those methods.

Setup:

Upvotes: 0

Views: 148

Answers (1)

TiM
TiM

Reputation: 16021

Overriding init directly isn't possible in Realm.

You can get around this by marking your init as a convenience one.

import Foundation
import RealmSwift

class Genre: Object {
    dynamic var id: Int
    dynamic var name: String

    convenience init(id: Int, name: String) {
        self.init()            
        self.id = id
        self.name = name
    }
}

Upvotes: 2

Related Questions