nil
nil

Reputation: 63

Swift Failable Initializer Can’t Return Nil?

I’m trying to return nil in my failable initializer but keep getting the error “All stored properties of a class instance must be initialized before returning nil from an initializer". Why?? Isn’t the whole point of failable initializers being able to return nil and stop initialization?

Upvotes: 3

Views: 352

Answers (1)

nazmul idris
nazmul idris

Reputation: 442

Apparently it’s a bug that’ll be fixed as of Swift 2.2 https://stackoverflow.com/a/26497229/5792198

In the meantime, you can initialize all the properties with dummy data and set a flag. At the end of all the init code check the flag and return nil.

Upvotes: 3

Related Questions