Realm-cocoa: RealmSwift is requiring Realm also

Created on 21 Apr 2017  路  2Comments  路  Source: realm/realm-cocoa

Goals

I posted this on StackOverflow http://stackoverflow.com/q/43500536/7542315 and was asked to post here as well. I am migrating an existing project to Swift 3 and have run into an issue where Xcode is giving errors about required inits for Realm. These inits use RLMRealm instead of Realm and require importing Realm as well.

Expected Results

RLMRealm initializers should not be required when using RealmSwift

Actual Results

Xcode error says 'required' initializer 'init(realm:schema:)' must be provided by subclass of 'Object', however these inits use RLMRealm and require importing Realm
See example here: http://stackoverflow.com/q/43500536/7542315

Steps to Reproduce

check http://stackoverflow.com/q/43500536/7542315

Code Sample

See http://stackoverflow.com/q/43500536/7542315

Version of Realm and Tooling

Realm framework version: 2.6.1

Xcode version: 8.3.2

iOS/OSX version: iOS 10.3.1

Dependency manager + version: Cocoapods 1.2.0

T-Help

Most helpful comment

Those initializers are required to be implemented by Realm so that we can construct objects for you from values using APIs such as Realm.create(_:value:update:) among others.

However, you don't need to ever see those if you create "convenience" initializers such as is documented here: https://realm.io/docs/swift/latest/#adding-custom-initializers-to-object-subclasses

Copied here for your _convenience_ 馃槈

When creating your model Object subclasses, you may sometimes want to add your own custom initialization methods for added convenience.

Due to some present limitations with Swift introspection, these methods cannot be designated initializers for the class. Instead, they need to be marked as convenience initializers using the Swift keyword of the same name:

class MyModel: Object {
    dynamic var myValue = ""

    convenience init(myValue: String) {
        self.init() //Please note this says 'self' and not 'super'
        self.myValue = myValue
    }
}

All 2 comments

Those initializers are required to be implemented by Realm so that we can construct objects for you from values using APIs such as Realm.create(_:value:update:) among others.

However, you don't need to ever see those if you create "convenience" initializers such as is documented here: https://realm.io/docs/swift/latest/#adding-custom-initializers-to-object-subclasses

Copied here for your _convenience_ 馃槈

When creating your model Object subclasses, you may sometimes want to add your own custom initialization methods for added convenience.

Due to some present limitations with Swift introspection, these methods cannot be designated initializers for the class. Instead, they need to be marked as convenience initializers using the Swift keyword of the same name:

class MyModel: Object {
    dynamic var myValue = ""

    convenience init(myValue: String) {
        self.init() //Please note this says 'self' and not 'super'
        self.myValue = myValue
    }
}

I answered on StackOverflow and it was accepted. I think this issue seems to be closeable. Feel free to reopen if you have further questions.

You don't need to override init(realm:schema:) and init(realm:schema:), just define convenience required init(node:in:) throws like the following.

class BaseModel : RealmSwift.Object, MappableBase, Identifiable {

    convenience required init(node: Node, in context: Context) throws {
        try self.init(node: node, in: context)
    }

    ...

}
Was this page helpful?
0 / 5 - 0 ratings