It has happened to us all: you’re in a storyboard, you’re using the Assistant Editor, you want to control-drag from a button into your code and create an action.
But sadly you forget to select “Action” from the drop down menu and instead create an outlet.
No biggie you think, deleting the outlet code. You go back and create the action properly, run the app… and get an error message such as this one:
*** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key badAction.’
Yikes! Unless you’re using Version Control, there is no way to undo adding that bad outlet. So you ask yourself: Will I have to start from scratch, or is there a way to fix your app?
Yes this is fixable – all we need to do is examine our storyboard file in Source Code mode, then find the problem and eliminate it. Here’s how you do it:
[emember_protected]
- With the left pane open (Navigation Inspector, showing all your files), select your storyboard and right click/control click to bring up the context menu. Select View as Source Code
This will allow you to see what the storyboard actually looks like in XML. Hit Option+F to bring up the Find in Context menu. Now search for whatever is causing you a headache, then delete the entire line. Make sure the opening and closing brackets are selected too.
In my case, all this needs to go:
1 |
<outlet property="badAction" destination="4R8-F9-Xhb" id="8xN-t2-22c"/> |
You may see more than one occurrence of your action in this file – we only want to get rid of the “outlet”reference here. If you delete all occurrences it’s not a big problem, but you will have to re-connect your actions to the code later.
[/emember_protected]
Once you’re done, run the app again and all should be fine.
To display the storyboard in the visual editor again, just control-click the file file again, and select “Open as – Interface Builder, iOS Storyboard”.
Alternatively (and this is even easier) you can just delete the connection from the Connections Inspector in Interface Builder (doh!)
Thanks. Worked like a charm!
Ho do i fix it if i have not a outlet’s name? my error like this:
‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key .’
Hi Dajrone, it’s difficult to diagnose without seeing more of your code. One of the reasons this can happen is when you set a property on an instance that doesn’t have it; for example, say you’ve written a subclass with a property, but you’re accidentally instantiating its parent class and try to set that property. To hunt down where the problem happens, set an exception breakpoint and see where Xcode stops your code.
Hello Versluis,
i am using storyboard and when i tap on the profile button my app crashes only on 4s device.
even not crashes in the simulator. how to fix i am not getting please help.your any suggestion would be highly appreciated.Thanks
here is my code :
– (IBAction)profileButtonTapped:(id)sender
{
HomeViewController *homeView = [self.storyboard instantiateViewControllerWithIdentifier:@”gotoProfileVC”];
[self presentViewController:homeView animated:YES completion:nil];
}
here is crash log:
Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.’
please ignore the previous crash log:
Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.’
Hi Yash, those things are tricky to find. It may be something in your storyboard rather than your code. Have you tried setting an exception breakpoint? Those will sometimes stop your app at the line of code that’s upsetting Xcode. That’s all I can suggest.