How to fix “this class is not key value coding-compliant” after accidentally adding an IBOutlet instead of an IBAction

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:

  • 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

badAction1

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:

badAction2
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.

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”.

About Jay Versluis

Jay is a medical miracle known as a Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

2 thoughts on “How to fix “this class is not key value coding-compliant” after accidentally adding an IBOutlet instead of an IBAction

Leave a Reply to Chris Cancel reply