How To Rename IBOutlet Variable Or IBAction Function Without Lose Connection In Swift

When I develop the iOS app user interface, I add one button and one activity indicator in the main storyboard file. Because I want to write swift source code to manage the button and the activity indicator, so I press the Control key and Drag & Drop the button and the activity indicator from the main storyboard view controller scene to the code assistant window, then it creates two @IBOutlet variables in the ViewController class file ( ViewController.swift ).

Now I can refer to the two UI components in the Swift source code. This is the process of connecting UI components with @IBOutlet variables, you can also use this method to create an @IBAction function to process UI component events. Please refer article How To Connect Storyboard UI Component To ViewController Class Code to learn more.

1. This Class Is Not Key-Value Coding-Compliant For The Key ActivityIndicator Error.

  1. But sometime after I rename an @IBOutlet variable or @IBAction function in the ViewController class source code, when I run the app, it will throw an error message like below.
    019-12-18 09:07:18.441998+0800 iOSActivityIndicatorExample[1319:21404] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<iOSActivityIndicatorExample.ViewController 0x7f9dfe40c990> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key activityIndicator.'
  2. This is because the @IBOutlet variable / @IBAction function’s name is changed, but the old @IBOutlet variable / @IBAction function name is used in a lot of places in the Xcode project files. So the connection between the UI component and the @IBOutlet variable / @IBAction function has been lost.

2. How To Rename An @IBOutlet Variable / @IBAction Function Correctly To Avoid Lose Connection Error.

There are two methods for you to rename an @IBOutlet variable / @IBAction function correctly in Xcode.

2.1 Use Refactor —> Rename Menu Item.

  1. Click the @IBOutlet variable ( activityIndicator ) / @IBAction function name to select it in swift source code.
  2. Right-click the mouse key, then click the menu item Refactor —> Rename in the popup menu list.
  3. Now it will show a window to highlight all the places ( project files ) where this @IBOutlet variable / @IBAction function name is used. In this example, the old variable name ( activityIndicator ) is only used in Main.storyboard file.
  4. Now change the highlighted old variable name activityIndicator to activityIndicator1 in the window, and click Rename button ( a blue button at the rename window top right corner ), then it will replace all the old variable names with the new variable name. Do the same to rename the @IBAction function name.

2.2 Find Old @IBOutlet Variable / @IBAction Function Name And Replace Them Manually.

  1. Right click the old @IBOutlet variable / @IBAction function name in ViewController.swift file.
  2. Click Find —> Find Selected Text in Workspace menu item.
  3. Then it will list all the files that contain the @IBOutlet variable / @IBAction function name text in the left result panel, and the name string is highlighted. Then you can change the variable or function name manually carefully. This method is not as good as the first one.

3. How To Fix The Error’nsunknownkeyexception’, Reason: … This Class Is Not Key-Value Coding-compliant For The Key X” In Xcode?

3.1 Question.

  1. I want to add a link between a UILabel object and an IBOutlet in swift class using Xcode.
  2. But when I do that, the iOS app crashes, and it shows the below error messages.
    *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x6e36ae0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key XXX.'
  3. Can anybody tell me how to fix the issue? Thanks a lot.

3.2 Answer1.

  1. There may have 2 reasons for such kinds of errors.
  2. The first reason is that maybe you have changed the IBOutlet property variable name in the .m/.h file, but the old IBOutlet variable has already connected to the UILabel, this cause Xcode can not find the original IBOutlet variable.
  3. The second reason is maybe there is a wrong connection between the UILabel and the IBOutlet variable in the xib file.
  4. You can solve the issue follow the below steps.
  5. Click the Xcode project’s Main.storyboard file to open the Interface Builder and select the UILabel object.
  6. Click the Connections Inspector tab icon on the Xcode window right side.
  7. Go to the Referencing Outlets section, and check whether there are UI components still connect to the old IBOutlet property variable.
  8. If you find there are references between the UI component and the old IBOutlet property, click the ‘x’ icon to remove the reference.
  9. Build the Xcode project again, you will find the error will disappear.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.