Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Failable Initializer Can’t Return Nil?

Tags:

ios

swift

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?

like image 587
nil Avatar asked Jan 20 '26 05:01

nil


1 Answers

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.

like image 55
nazmul idris Avatar answered Jan 23 '26 00:01

nazmul idris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!