When running an iOS app from Xcode, especially one that uses Storyboards or XIB files, the app may crash with an exception containing this class is not key value coding-compliant for the key ….
This error usually means Interface Builder is still trying to connect an outlet or action to a property that no longer exists, or that the Storyboard/XIB object is assigned to the wrong custom class. This guide explains the common causes, how to identify the offending key, and several practical fixes.
- Typical error message
- Common causes
- Typical scenario
- How to identify the problem
- Fix 1: Remove stale IBOutlet connections
- Fix 2: Make the IBOutlet name match the code
- Fix 3: Verify Custom Class and Module
- Fix 4: Inspect the Storyboard or XIB source
- Fix 5: Clean the build and remove DerivedData
- Fix 6: Recheck duplicated cells and views
- Important notes
- Summary
Typical error message
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyViewController 0x...> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key titleLabel.'
The final key, such as titleLabel, is the name Interface Builder is attempting to set on the object.
Common causes
- An IBOutlet was deleted or renamed in code while the Storyboard/XIB connection remained.
- A copied view, cell, or view controller still contains old outlet connections.
- The Custom Class or Module in Interface Builder points to the wrong class.
- A UITableViewCell or UICollectionViewCell is using a class that does not contain the expected outlet.
- A Swift or Objective-C runtime name no longer matches what the archived Interface Builder file expects.
Typical scenario
Suppose a view controller originally contained this outlet:
@IBOutlet weak var titleLabel: UILabel!
If the outlet is later removed from the Swift file but the Storyboard connection is not removed, the app can still build successfully. It then crashes at runtime when the Storyboard tries to assign a value to the nonexistent titleLabel key.
How to identify the problem
- Read the crash log and note the key name shown at the end of the exception.
- Open the relevant Storyboard or XIB file.
- Select the affected view controller, view, cell, or other object.
- Open the Connections Inspector.
- Look for missing, broken, or obsolete Outlet and Action connections.
- Open the Identity Inspector and verify the Custom Class and Module.
Tip: The key name shown in the exception is usually the fastest clue. Search that exact name across your Storyboard/XIB files and source code first.
Fix 1: Remove stale IBOutlet connections
This is the most common solution. In Interface Builder, select the affected object and open the Connections Inspector. Remove any outlet or action that points to a property or method that no longer exists.
Use the small “x” next to the obsolete connection, then reconnect the UI element to the correct property if needed.
Fix 2: Make the IBOutlet name match the code
If the Storyboard connection is still needed, make sure the property name in code matches the connection.
@IBOutlet weak var titleLabel: UILabel!
If a refactor changed titleLabel to nameLabel, reconnect the Storyboard outlet to the new property rather than leaving the old connection in place.
Fix 3: Verify Custom Class and Module
Open the Identity Inspector and confirm that the Storyboard or XIB object is assigned to the correct class.
class ProfileViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
}
If the Storyboard object is assigned to a different view controller class that does not define titleLabel, the same runtime exception can occur. Also verify the Module setting, particularly in projects with multiple targets or frameworks.
Fix 4: Inspect the Storyboard or XIB source
If the broken connection is difficult to locate in Interface Builder, open the Storyboard or XIB as source code and search for the key name.
<outlet property="titleLabel" destination="ABC-12-XYZ" id="..."/>
An obsolete connection can sometimes remain in the XML. Whenever possible, remove or recreate the connection through Interface Builder rather than manually editing the XML, because malformed Storyboard XML can make the file unusable.
Fix 5: Clean the build and remove DerivedData
After fixing the connection, run Product → Clean Build Folder. If stale build artifacts still appear to be involved, remove DerivedData and rebuild.
rm -rf ~/Library/Developer/Xcode/DerivedData/*
Restart Xcode afterward and build the project again.
Fix 6: Recheck duplicated cells and views
Copied UITableViewCell, UICollectionViewCell, and custom UIView objects can retain outlets from the original object. Check the Reuse Identifier, Custom Class, Module, and Connections Inspector together instead of looking at only one setting.
Important notes
This is usually a runtime Interface Builder problem, not a Swift compiler error. The project may compile without warnings and still crash only when the affected screen, cell, or XIB is instantiated.
Changing an outlet from implicitly unwrapped to optional does not fix a stale Storyboard connection. The real issue is that Interface Builder is trying to set a key that the runtime object does not have.
Summary
The Xcode error this class is not key value coding-compliant for the key … is most often caused by an outdated connection between a Storyboard/XIB file and your code. Start with the key named in the exception, then inspect the Connections Inspector, Custom Class, Module, and any duplicated views or cells.
Remove obsolete outlets, reconnect renamed properties, and verify the correct class assignment. If the problem persists, inspect the Storyboard/XIB source and then clean the build folder or DerivedData. Working through those checks in order usually reveals the mismatch quickly.
