How To Fix Swift Unrecognized Selector Sent To Instance Error

When I develop an iOS example application use swift, I meet a strange situation. When I click the run button to start my iOS example, it runs well. My example iOS app has a button, so I click it to trigger a Touch Up Inside event for this button, but this action makes my iOS app stop at the first line of class AppDelegate, I know AppDelegate class is the entry point for every iOS app, so I just click the Step over button to continue to run the example.

But I found the swift code execution does not continue, it stops here and the app hangs. When you click the Step over or Continue program execution button, the program execution will return to the first line of AppDelegate class, and the event process function code will not be invoked. This confused me a lot of times.

So I think maybe there are some exception occurred. I follow the article How To Debug And Get Error Messages In Xcode Step By Step to find out the error messages like this unrecognized selector sent to instance. Below is the full error messages stack trace.

2019-06-27 14:37:30.286038+0800 ManageSubViewExample[1443:29617] -[ManageSubViewExample.ViewController changeSubViewTransparence:]: unrecognized selector sent to instance 0x7fb7b9d0c880
2019-06-27 14:37:30.297875+0800 ManageSubViewExample[1443:29617] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ManageSubViewExample.ViewController 

changeSubViewTransparence:]: unrecognized selector sent to instance 0x7fb7b9d0c880'

*** First throw call stack:

1. How To Fix Unrecognized Selector Sent To Instance Error When Click Button.

  1. After investigating, I finally find out the reason.
  2. This is because I had connected two functions to the button’s Touch Up Inside event and I remove one old function source code from ViewController.swift file because I do not use it anymore.
  3. But the connection between the button and the removed function still exists. So I need to remove the connection between the UI button and the removed function also.
  4. Please follow the below steps to find out the connections between the button and related functions.
  5. Click Show the Project navigator button ( the folder button at the leftmost ) in Xcode left panel to display all project files.
  6. Click Main.storyboard file to open it in the designer.
  7. Select the UI button component in the designer.
  8. Then click View —> Inspectors —> Show Connections Inspector menu item at top menu bar. This will open the connections inspector window for the button at Xcode right panel.
  9. You can see there are two functions that have been connected to the Sent Events —> Touch Up Inside event. You should click the close button on the unused function left side to remove the connection.
  10. Now rebuild your Xcode project, and the error should be fixed.
  11. But if you find the above method does not take effect, and the error still exists, then remove all the two connections for the button, and reconnect the UI button with the correct function again, then the error should be fixed. You can read the article How To Connect Storyboard UI Component To ViewController Class Code to learn more.

References

  1. iOS Add Click Event To UIButton Swift Example
  2. How To Display Xcode Library, Assistant Editor, Inspectors Window
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Pushka

wow, thanks!
Hours of researching, this one really worked

TheONE
TheONE

Great tutorial! Thank you for saving my time!

Tony J
Tony J

Thank you!!!!! I spent hours trying to debug my code to resolve this and 10 minutes after reviewing your page I have the fix

3
0
Would love your thoughts, please comment.x
()
x