Search code examples
iosswiftrealm

Swift and Realm: Invalid use of '()' to call a value of non-function type 'module<Realm>'


I am literally copying and pasting code from the official Realm documentation for Swift - and it doesn't compile:

https://realm.io/docs/swift/latest/#adding-objects

enter image description here

enter image description here I am using Realm 1.0.2

The documentation is really conflicting with itself - with varying declarations of realm:

let realm = RLMRealm.defaultRealm()
let realm = Realm()
let realm = try! Realm()

What's going on with Realm?

And what's the difference between Realm and RLMRealm ? The first line above will compile fine - but the next two will NOT.


Solution

  • I am literally copying and pasting code from the official Realm documentation for Swift - and it doesn't compile:

    The code snippet shown in your screenshot is missing the parentheses needed to call the initializer of the Realm type. I've skimmed the documentation page you linked to, and from what I can see all uses of the Realm initializer correctly have their parens.

    The documentation is really conflicting with itself - with varying declarations of realm:

    I'm not sure which documentation you're referring. The only form I see used in the Realm Swift documentation is let realm = try! Realm(), which is the correct form to use with Realm Swift.

    And what's the difference between Realm and RLMRealm?

    The Realm framework provides an Objective-C API, with the RLMRealm class representing the Realm file. The RealmSwift framework provides a Swift API, with the Realm class representing the Realm file.

    The first line above will compile fine - but the next two will NOT.

    This reveals that you're using the Realm Objective-C framework from Swift (import Realm), rather than Realm Swift (import RealmSwift).