unrecognized selector sent to instance
This is a topic that many people are looking for. thevoltreport.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, thevoltreport.com would like to introduce to you Common Xcode Errors – Unrecognized Selector Error. Following along are instructions in the video below:
hello in this video Im gonna show you guys another common error message and what might be the cause of it so let me first go through the code that I have here I have an iboutlet which is an image view and its called my image view so this property or this outlet is connected to a UI image view element in my storyboard if youve gone through my first 15 beginner lessons on my youtube page youll know that you know you can add elements onto your storyboard like I have here a UI image view thats used to display an image and you can connect them to the code file or the swift file and then you can use code to set the properties of it so what am I doing here this is the second line that I want to show you guys Im referencing that image view and it has an image property and Im setting that image property to a new UI image object Im just setting some random name of an image that doesnt exist but thats irrelevant so what Im trying to do here with this line is set the image of the image view so that I can display some sort of image and when I run this I get an error in the console window here you dont have this console window make sure you have this lower button here highlighted so it shows it if you dont see this area at all down
here click this icon here to reveal that pane okay so the error message is unrecognized selector sent to instance something and selector is another name for method or function so what its saying is that it doesnt recognize the function that youre trying to call on this object to put it in plain words and it tells you right here that youre trying to call the set image method here of an object that is a uibutton well thats really strange because I could have sworn that my property is a type of uiimageview and I know for a fact that uiimageview has a method called set image or when you set the image property its actually behind the scenes its calling the set image method so thats why its telling you that it has this error well this error message says that youre trying to set youre trying to call this set image method on a button so that tells you that maybe the element that is connected to this property even though we think it is a uiimageview is not actually and I see this happen a lot because in the storyboard you may have elements overlapping each other and its kind of hard to tell so when you are connecting your IB outlets when youre connecting these elements to the swift file so Im going to click the assistant editor button here it shows you side-by-side so that you can click and drag and connect your elements to the right-hand
sides of the code file here so what must have happened is I must have accidentally highlighted the button and held down ctrl and drag it over here and connected my button but I named it the image view thinking that I was dragging the image view so thats where the error is in fact if I right click the button you can see down here in referencing outlets that this button is connected to the my image view property of this view controller so I just want to click the X and break that connection so you can see here now that this grey dot is empty is an empty circle meaning that this property is no longer connected or this outlets no longer connected so one surefire way of connecting outlets is instead to use this document outline and you can select the element from this listing and it hold down control and drag it all the way here and then create your outlet that way so you dont see this document outline in your storyboard theres a button down here you can hide it and show it like that so now that weve already written this outlet what if I dont want to create a new one because right now its not connected to anything right theres an empty grey circle so what I can do is I can right click or command + click if you have a one-button mouse the image view and then down here in referencing outlets
here to the circle drag you dont have to hold down control you just click and drag it drag it to the view controller up here let go and you can see here that you can connect it to the my image view property so Im going to do that now you can see that under referencing outlets this image view element is connected to the my image view property like that and now on the side you can see that this circle is highlighted in gray solid gray meaning that its connected so now my app wont crash because its going to call and just run it its going to call set image on a UI image view element which is what we expected but unfortunately you also wont see any image because I dont actually have an image called my image name but you can see that theres no crash now so if you see an error message in your console that says something like unrecognized selector sent to instance something dig around in the console to find out what element is actually connected to that property in my case I was trying to call the set image method on a button and in the console area I could see that that outlet was connected to a button rather than a UI image element as I was expecting it to be so I hope that saves you guys some time in debugging your own apps and Ill see you guys next time you
XCode Tutorial, XCode, iOS Tutorial, iPad, iPhone, Develop, Create, Build, Code, How To, iOS, iPhone Apps, iPad Apps, Objective C, Developer, Introduction, B…
Thank you for watching all the articles on the topic Common Xcode Errors – Unrecognized Selector Error. All shares of thevoltreport.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.