Friendly Code Friendly Code -5 years ago 113
Swift Question

XCode project won't build (Terminating app due to uncaught exception)

I have an XCode project with a main storyboard and main ViewController. There are then 4 sub classes/xib files which can be swiped as pages. This is all working fine, but as soon as I add a label and link it up as an IBOutlet the app does not build successfully and I get this error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7ff7cc0953b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key daysCountDaysLabel.'

I've been stuck on this for hours and followed solutions from 10 or so similar (and resolved!) posts on Stack Overflow without it working for me.

Things I have tried:

  • Making sure there are no IBOutlets that don't match labels

  • Removing all the labels/IBOutlets, testing it works and then adding
    a new label and matching up to an IBOutlet (which then doesnt work)

  • Cleaning the build

  • Checking the source for the .xib files to see if there any old outlets etc

  • Making sure class names match up

  • Stopping myself posting on Stack Overflow with something that seems easy to fix as there are so many other resolved posts with the same issue (sorry)

I have zipped up the project and placed here in case anyone can identify and solve the issue:

Answer Source

Well I went through the attached file and I think this might be the problem:

let vc0 = UIViewController(nibName: "ViewControllerDays", bundle: nil)

You are instantiating a UIViewController when it should have been ViewControllerDays

So that is why it fails at runtime as its trying to access it as a UIViewController which does not have any of these properties.

And this happens in a lot of places. You need to init with the proper view controller class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download