Bye Storyboards

Storyboards are awesome and if I was doing Objective-c, I would probably still be using them. But the thing with Swift and Storyboards, is that it shows that there is still some friction between the language and Cocoa.

Image you have this:

class MyAwesomeVC: UIViewController {  
  let bar : Bar

As you might have guessed, this will make the compiler unhappy, because you haven't initialized bar:

Property '' not initialized at super.init call

But now is where the problem begins. If you are using a Storyboard, it will initialize the UIViewController for you. So where/how do you set bar's value?

If you are able to create bar without relying on anyone else, you can:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder) = //

If you need to pass something, you will most probably try to do it on prepareForSegue. That's ok, but it forces you to change your property to one of the following:

  1. var bar : Bar?
  2. var bar : Bar!
  3. private(set) var bar : Bar? // + custom setter
  4. private(set) var bar : Bar! // + custom setter

On the other hand, without a Storyboard, you can go with
init(nibName: nibNameOrNil, bundle: nibBundleOrNil) and customize it:

required init(nibName: nibNameOrNil, bundle: nibBundleOrNil, bar: Bar) {  
     super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) = bar

Although I lose some of the nice things a storyboard offers, I prefer to have cleaner and safer properties.