I read What exactly is init coder aDecoder?
but that doesn't answer to why not put everything inside awakeFromNib and forget using init(coder aCoder : NSCoder)?
In the comments of the accepted answer Fattie says:
"sometimes you can't do that". You typically can but not always
Can anyone provide more explanation to that?
If you have
letsthat need to be initialized in aninit, you have to use that instead ofawakeFromNib.Doing so allows you to avoid implicitly unwrapped optionals.
EDIT:
If you want your class to have properties, you can either do
or
The first is preferable because it is safe. In the second, you run the risk of accessing
abefore it is initialized.But, to make sure
ais always initialized, it needs to get its value in aninitof the class.So,
If you don't have an init, then you can't have an init that gets initialized later.