iOS开发报错:uncaught exception 'NSUnknownKeyException'排查求助
NSUnknownKeyException for "description" Hey there, let's dig into this tricky NSUnknownKeyException issue you're facing—those can pop up even when you swear you never touched the mentioned key! Let's break down the most likely causes and how to check them step by step:
1. Check Storyboard/XIB Connections (Most Common Culprit!)
Even if you didn't write code calling "description", Interface Builder links are the #1 cause of this error. Here's what to do:
- Open the storyboard or XIB tied to your
ViewController. - Select the
ViewControllerobject (the yellow icon in the document outline). - Switch to the Connections Inspector (right panel, icon looks like a circle with an arrow).
- Scan all Outlets and Actions—look for any links to a property/method named
description, or broken grayed-out connections. Accidental drag-and-drops often create these without you noticing. - Don't forget to check individual UI controls (buttons, labels, etc.) too—any of them might have an incorrect outlet linked to
description.
2. Scan ViewController.h and .m for Hidden References
Even if you didn't intentionally use "description", double-check for these easy-to-miss issues:
- Did you accidentally declare a property named
descriptioninViewController.h? For example:
This clashes with the system's built-in@property (nonatomic, strong) NSString *description;descriptionmethod fromNSObject, which can trigger unexpected KVC calls that throw the exception. - Look for any KVC-related code: Calls like
[self valueForKey:@"description"]or[someObject setValue:foo forKey:@"description"]might be hidden in your code, even if you didn't mean to write them. - Check for old-style
@synthesizestatements referencing "description"—modern Obj-C auto-synthesizes, but legacy code might still have this.
3. Check main.m and Launch Setup
While less likely, main.m could have code that indirectly triggers this:
- Are you doing any runtime manipulation of
ViewController? Like usingobjc_setAssociatedObjectwith the key "description"? - Is there code loading a nib or storyboard programmatically that might be passing an incorrect key?
4. Clean and Rebuild Your Project
Cached build files often cause weird, unexplainable issues. Try these steps:
- Clean your build folder: Go to
Product > Clean Build Folder(hold the Option key to see this option). - Delete derived data: In Xcode, go to
Xcode > Settings > Locations > Derived Data, click the arrow to open the folder, then delete your project's folder. - Rebuild and run the project again.
If none of these fix things, sharing a snippet of your ViewController header and the full exception stack trace would help narrow it down further—stack traces usually show exactly where the problematic KVC lookup is happening!
内容的提问来源于stack exchange,提问作者KP.

