Search code examples
swiftclassself

What is .self after a class name used for? Swift 3


I recently noticed that some codes do init of instances like ClassName.self() for example:

let realm = try! Realm.self()

From the output and the usage thereafter, it is just like as though without it - Realm()

Is there any specific reason or usage for doing an init with .self?


Solution

  • This is somewhat speculation, but I believe the places that do use Type.self() in the context of Realm follows a misperceived convention that has followed (some irrelevantly) the effects of Swift team resolving the bug

    The following commit by the Realm team was in preparation for the anticipated resolution of the bug above:

    It's possible that the changes in the commit above (Type updated to Type.self, in proper context) has inspired code bases that make use of Realm to also make use of Type.self in initializer context, i.e., Type.self(). This is, however, a redundant use of the .self suffix.