Please fix initializer woes when using NSCoder

Hello, I apologize if I am not submitting this correctly. I've been working with Swift for a little while now, and for the most part I like it a lot. But one thing keeps popping up and is incredibly inelegant to deal with. When you have a complicated initializer for a class, and you want the class to support NSCoding, there seems to be no easy way to have that initialization code accessible from both the normal initializer and the NSCoding initializer. Duplicating code is a big no-no in programming and yet Swift practically forces you to do this. I'm sorry that I don't know enough to suggest a remedy, but I just wanted to voice a plea that this issue be resolved, if possible. Does anyone know if there are plans to do that?

Also -- it's weird to me that Swift has so many convenience features but in some ways they make things more cumbersome than other languages. Specifically, it's annoying that 'if' statements MUST have brackets when there's only one line of code to be executed. I'm sure there's a good reason for it but it seems backward in practice compared with all of the other space and time saving features.

Thanks
Bob

There has been some discussion in the past about the possibility of ā€œinit helperā€ methods, though to my knowledge nothing has yet come of it. Do you have a suggestion for improving the situation?

I imagine ā€œgoto failā€ might have something to do with that. Personally, I like the consistency of having brackets demarcate all code blocks.

1 Like

If you're in Phase 1 of initialization, you have to return values from static methods and assign them to properties. In Phase 2, you can just use an instance method and do any mutations directly. None of that is as clear as it could be, but I don't think it's "not easy".

As for the if brackets, I think this is due to Swift not requiring parentheses around the condition. I love that standardization.

@Chris_Lattner3 has mentioned "two-phase initialization" before (probably others too). Perhaps he can illuminate what was meant by that?

But yes, this is especially painful for things like UIViewControllers from storyboards. Loading them from xib files is a bit better but you still have the coder initializer hanging around unimplemented.