Saturday, August 30, 2014

So how are those special Swift initializers working out?

Splendidly:
If you're building a UIView subclass that needs to set up a mess of subviews this can get old really quick. Best option I've found so far? Just initialize them with a default value like you would a regular variable. Now the compiler's off your back and and you can move on with your life, or at least what's left of it after choosing software development as a career.
Justin Driscoll

This is something people who create elaborate mechanisms to force people to "Do the Right Thing" never seem to understand: they hardly ever achieve what they are trying to achieve. Instead, people will do the minimal amount of work to get the compiler off their backs. Compare Java's checked exceptions.

1 comment:

Justin Driscoll said...

Thanks for the link! I do think it's a hard problem and I probably come off as more negative than I really am. To me the easiest thing would be to just define all properties as forced unwrapped optionals like they do with IBOutlets. If you do this you lose the advantage of having non-optional variables though and open yourself up to crashes. My take, for now at least, is that assigning a default value is the easiest way to maintain safety while not writing multiple painfully long initializer methods.