[UILabel copyWithZone:]: unrecognized selector sent to instance

穿精又带淫゛_ 提交于 2019-11-28 07:07:38
Aleks G

I guess this was one of those weird bugs with Xcode that you can't trace or reproduce every time. I narrowed the issue down to one specific UILabel in the IB. As soon as I connect it to an IBOutlet UILabel *, I get this error.

On further investigation, it turns out that name title was causing the issue. I had the label declared as

IBOutlet UILabel *title;

As soon as I changed the name to gtitle, everything worked as expected. Who knows?..

Try this :

self.view.backgroundColor = [UIColor clearColor];
self.view.layer.backgroundColor = [[UIColor alloc] initWithPatternImage:img].CGColor;

This definitely looks like an "over released" issue.

The reason is hard to tell in your example (since the code looks absolutely fine) but the fact that you're having a copyWithZone selector sent on an instance of UILabel (instead of UIColor) would definitely tell me that the bg has been over released and is now referring to a UILabel.

Check that later on in the code you do not again release the object bg.

Hope this helps

EDIT AFTER YOUR FIRST COMMENT

This is quite weird that you're receiving a copyWithZone unrecognized selector sent to instance, because I would believe that if your self.view was of the wrong type (not a UIView) then you would receive a setBackgroundColor unrecognized selector sent to instance and not a copyWithZone.

To better analyze this problem, when the exception is raised, I would write into the debugger:

po [self view]
po [[self view] backgroundColor]

to see if everything is working out smoothly as it should be.

this issue happened to me last night and I actually did have a label named title. I tore a bit of my hair out when changing the name to something else didn't work. It turns out that I was attempting to send a NIL value from a text field to a label in a VC. I have 4 VC's and a user enters a title at VC 1 in the text field and that gets stored into an object of type NSString. That string is put into a label in VC 4. When a user doesnt enter a title in VC 1, that's where the NIL came in . I fixed it by assigning a default value to the string object when the user doesn't enter a title in VC1. Hope that helps someone.

I had a similar issue that I managed to solve using the answers provided here.

In my case, however, I had a class called UIDateLabel (used to present dates in a formatted way etc). Compiling and running resulted in the NSInvalidArgumentException mentioned above.

I WAS suspecting that maybe there is a class called like that, but googling on UIDateLabel only showed some obscure hits (and none that linked to Apple as I could see). So I assumed that there was no naming problem for a while... until I read the answers in here and gave it a try.

So I replaced the name to UIDateLabelAbc and everything works perfectly. Renaming back to UIDateLabel does not work as XCode tells me that there is a class with that name already. So I did some more googling and found some wiki page that claims to list the UIKit.framework inheritance hierarchy, and in that list I found UIDateLabel listed. However, that's all the info I managed to find about that class, i.e. that it exists. What it does, its purpose and so on remains unknown to me at the moment.

Update: After reading about naming conventions I now realize that prefixing your classes with UI is not good practice (to say the least). So, don't prefix your classes with UI and you'll avoid this and probably a bunch of other problems.

I get this error when I declare an IBOutlet variable, and give it the same name as something else in that view controller. Renaming my variables and functions more sensibly therefore solved the issue

Swift 5 in my Case

YOURBUTTON.addTarget(self, action: #selector(funCross), for: .touchUpInside)

//MARK:- My Function 
@objc func funWithSearch(screen: String)
{

}

if you focus on #selector(funCross) i am calling a function without object

when i use this its work fine

YOURBUTTON.addTarget(self, action: #selector(funTemp), for: .touchUpInside)

//MARK:- My Function 
@objc func funTemp()
{
    funWithSearch(screen: "etc your desire string")
}

//MARK:- My Function 
@objc func funWithSearch(screen: String)
{

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!