3

For instance, there is a property in a view controller

@IBOutlet weak var nameLabel: UILabel!

This property is nil inside viewWillAppear and viewDidLoad, so the app crashes at runtime.

It was working fine in Xcode 6 Beta 4. After I switched to Beta 5, it complained about the controller class does not implement its superclass's required members. So I added

required init(coder aDecoder: NSCoder!) {
  super.init(coder: aDecoder)
}

And that compiler error disappeared. However, the app crashes for unexpectedly found nil while unwrapping an Optional value because that nameLabel property is nil when I try to set its text.

I read through the release notes and could not figure out how to fix this issue.

Ethan
  • 18,584
  • 15
  • 51
  • 72
  • Same [here](http://stackoverflow.com/questions/25150226/connected-iboutlets-are-not-initialized/25150932) Now I see that it isn't only my problem but I don't believe that apple submitted Beta5 with such major bug... – OgreSwamp Aug 06 '14 at 01:41
  • is your controller `UICollectionViewController`? – OgreSwamp Aug 06 '14 at 01:58
  • No, mine is subclassed from `UIViewController` – Ethan Aug 06 '14 at 03:06
  • I am having all weired problems with Beta 5. I think i will revert to Beta 4 and wait for Beta 6 – KD. Aug 06 '14 at 04:58
  • I was told the workaround by someone at Apple. See my revised answer below. – matt Aug 07 '14 at 18:41

2 Answers2

4

I was having the same issue in Beta5. It appears to be a problem where

init(nibName: nil, bundle: nil) 

is not mapping nil to the default nibName. When I changed to an explicit nibName then it worked. Specifically in my case, using the new ?? operator:

override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
   // beta5 workaround: replace nil with explicit name of xib file
   let nib = nibNameOrNil ?? "MyViewController"

   super.init(nibName: nib, bundle: nibBundleOrNil)

   // local initialization here
}

caused it to magically work again.

Spencer
  • 301
  • 2
  • 4
  • This is a nice workaround, because (1) it places the responsibility on the shoulders of the view controller itself, (2) it uses the cool new `??` operator. :) – matt Aug 06 '14 at 15:02
  • Ugh, I spent too much time battling with the compiler and then puzzling over this crash. Thank you. – brittlewis12 Aug 18 '14 at 07:13
  • This bug is fixed in iOS 9 beta 4, so this workaround become mercifully unnecessary. – matt Jul 22 '15 at 15:39
1

It's a temporary bug. The workaround turns out to be: Declare your view controller in such a way as to override name mangling, like this:

@objc(ViewController) ViewController : UIViewController { // or whatever its name is

See also: Are view controllers with nib files broken in ios 8 beta 5?

EDIT This bug is fixed in iOS 9 beta 4.

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141