24

I'm using a tutorial to write a fairly simple app. I copied the app to my work computer today. It was working perfectly on my laptop last night, but today I get this error. I tried gutting nearly all of the code, and even deleted everything in the didFinishLaunchingWithOptions function, but I still get this error. I don't even have a navBar declared anywhere! I assume there's some kind of project or file setting outside of the code that is the problem?

2011-11-22 14:39:32.294 LetsEat[15320:b603] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<LetsEatAppDelegate 0x5a824a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key navBar.'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00fa45a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x010f8313 objc_exception_throw + 44
    2   CoreFoundation                      0x00fa44e1 -[NSException raise] + 17
    3   Foundation                          0x0079f677 _NSSetUsingKeyValueSetter + 135
    4   Foundation                          0x0079f5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
    5   UIKit                               0x0021b30c -[UIRuntimeOutletConnection connect] + 112
    6   CoreFoundation                      0x00f1a8cf -[NSArray makeObjectsPerformSelector:] + 239
    7   UIKit                               0x00219d23 -[UINib instantiateWithOwner:options:] + 1041
    8   UIKit                               0x0021bab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
    9   UIKit                               0x0002117a -[UIApplication _loadMainNibFile] + 172
    10  UIKit                               0x00021cf4 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 291
    11  UIKit                               0x0002c617 -[UIApplication handleEvent:withNewEvent:] + 1533
    12  UIKit                               0x00024abf -[UIApplication sendEvent:] + 71
    13  UIKit                               0x00029f2e _UIApplicationHandleEvent + 7576
    14  GraphicsServices                    0x011dd992 PurpleEventCallback + 1550
    15  CoreFoundation                      0x00f85944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    16  CoreFoundation                      0x00ee5cf7 __CFRunLoopDoSource1 + 215
    17  CoreFoundation                      0x00ee2f83 __CFRunLoopRun + 979
    18  CoreFoundation                      0x00ee2840 CFRunLoopRunSpecific + 208
    19  CoreFoundation                      0x00ee2761 CFRunLoopRunInMode + 97
    20  UIKit                               0x000217d2 -[UIApplication _run] + 623
    21  UIKit                               0x0002dc93 UIApplicationMain + 1160
    22  LetsEat                             0x00001c99 main + 121
    23  LetsEat                             0x00001c15 start + 53
)
terminate called throwing an exceptionCurrent language:  auto; currently objective-c
(gdb)

EDIT: This is the top of my delegate.m file. As you can see it's all boilerplate:

#import "LetsEatAppDelegate.h"
//#import "ItemsViewController.h"

@implementation LetsEatAppDelegate

@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];
    return YES;
} 

I removed every other file in the project and still get this error.

EDIT 2

As it now stands, I only get this error on my work machine. The exact same files run perfectly on my home laptop. Same version of XCode (4.2.1)

Jon
  • 826
  • 1
  • 7
  • 25

8 Answers8

41

Reset Content and Setting didn't work.

Here's what wasted 2 hrs of my late night time:

I had connected an UIControl outlet in the interface builder to the IBOutlet in the xib's owner. For some reason, the IBOutlet was deleted from the owner, but the reference to the outlet remained dangling in the xib. This would always give me the error

"Terminating app due to uncaught exception 'NSUnknownKeyException'"

Lesson learnt: When deleting any outlets for vars in the implementation, make sure to unhook the respective connection in the IB

Update: (26 Jan 2015)

There's a good reason why the Interface Builder or the nibs behave this way, and I realized the why, today.

If you have an IBOutlet in TheBaseClass, and you have a nib or scene in the storyboard for a TheSubClass, there are two ways (hacks) available for you to connect this outlet:

  1. Under the Identity Inspector, name the "Custom Class" identifier to TheBaseClass, connect the outlet, then set the Class name in the "Custom Class" section to TheSubclass

  2. Add the IBOutlet in your TheSubclass code, connect it to the ui element in the nib, add the IBOutlet code in TheBaseClass, delete that code in TheSubClass

When I came across this answer it just fell in place, as to why Apple would let this dangling reference problem remain as is. Because, this is not a bug. This is a feature.

Nitin Alabur
  • 5,654
  • 1
  • 30
  • 51
  • The lesson I learned was: Don't use IB. – Jason Moore Jul 05 '13 at 12:53
  • 2
    agreed, IB wasn't as matured then, nor is it now. But it certainly is a zillion times better than 2 yrs ago. And XCode5 IB features will make it harder not to use IB, because its so damn awesome. Have moved from programmatic only to IB based development, and still swear by it :) – Nitin Alabur Jul 07 '13 at 00:18
  • You're my saviour! I've just started trying iOS development as an Android developer and this is bugging me for the past 4 hours! – Sri Harsha Chilakapati Mar 11 '21 at 21:07
15

Fixed - went to iOS Simulator > Reset Content and Setting

Jon
  • 826
  • 1
  • 7
  • 25
10

I had this issue and found that I had set the 'Main Interface' and it was causing an issue.

To change this setting click on your project (in the left hand file browser). The center area should now display the 'Summary' of settings for your project. This is the same area where you would change the iOS Application Target or the Supported Device Orientations. Under the section 'iPhone/ iPod Deployment Info' clear the 'Main Interface' drop down, leaving it blank. This fixed the issue for me.

Good luck.

RachelD
  • 3,912
  • 8
  • 38
  • 67
  • 2
    This helped me while working on an Ionic / Cordova app. Clearing out the value for Main Interface fixed it immediately. The error was not very descriptive of the problem. – newz2000 Feb 15 '16 at 19:19
  • I was working with ionic too and for some reason I set the Main Interface, after a couple of hours of wasted time found this thanks @newz2000 – marman Oct 07 '16 at 04:09
  • Same for me with a Cordova/Ionic2 app. I set this myself before though. – Rocco Nov 30 '16 at 22:03
9

The issue is in your nib file. There is an old connection from the LetsEatAppDelegate to the navBar.

I am not sure how your app is set up, there may be an LetsEatAppDelegate object in one of your nib files, or there could be a file owner with the class set to LetsEatAppDelegate

4   Foundation                          0x0079f5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
5   UIKit                               0x0021b30c -[UIRuntimeOutletConnection connect] + 112
6   CoreFoundation                      0x00f1a8cf -[NSArray makeObjectsPerformSelector:] + 239
7   UIKit                               0x00219d23 -[UINib instantiateWithOwner:options:] + 1041
8   UIKit                               0x0021bab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168


2011-11-22 14:39:32.294 LetsEat[15320:b603] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<LetsEatAppDelegate 0x5a824a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key navBar.'
*** Call stack at first throw:
Jesse Black
  • 7,898
  • 3
  • 31
  • 45
  • The funny thing is that this is basically a brand new project, Window based application. I definitely never created a Navbar, and I did a search and couldn't find a mention of navbar anywhere in my project. – Jon Nov 22 '11 at 21:27
  • 1
    Try opening the relevant xib in the interface builder, rightclicking on the File's Owner, and checking for dangling connections there. This sometimes happens to me as well... – Warkst Nov 22 '11 at 21:44
  • @Warkst thanks for clarifying that. @ Jon old connection=dangling connections – Jesse Black Nov 22 '11 at 21:48
  • So I got home to try that, and I don't have the error using the exact same code, both using XCode 4.x (will have to check the work version, I'm on 4.2.1 at home). – Jon Nov 23 '11 at 01:49
  • I'm now on 4.2.1 at home and work. I successful ran the project at home this morning, got to work, and it STILL crashes with this error. Exact same files, exact same version of XCode. – Jon Nov 23 '11 at 14:04
4

Oh man. I was having the same problem after removing @properties and/or IBActions and it was driving me crazy! Warkst was right on the money. Go to the problem view controller, click on the orange ball and deselect any dangling references you find. You may need to scroll down to find them all. This solved my problem. Thanks a million.

0

Clearing "Main Interface" also solved the issue for me. You may need to perform a "Clean" after clearing "Main Interface" and before building.

0

I meet this problem too,

1.In AppDelegate method

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

the rootViewController is init by this methods initWithNibName..... but actually the nib file is not exist, so the exception throw..

2.When viewController load view it crashes

I guess maybe because I deleted the xib file,then the view controller still init with nib file.. so I add the -(void)load view methods. and it is ok now..ps:I reset the simulator,but it doesn't work.

Hope this help.

Nagarjun
  • 5,811
  • 5
  • 28
  • 47
frank
  • 1,978
  • 1
  • 15
  • 19
0

I faced the same problem recently. All my outlet connections where perfectly fine though. I finally made it by making decent CLEAN of the project. Thanks to @Robert Wohnoutka and this post: How to Delete Derived Data and Clean Project in Xcode 5 and Xcode 6?

Community
  • 1
  • 1
Quentin Malgaud
  • 385
  • 5
  • 20