Xcode 中的错误“无法识别的选择器发送到实例”是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5455316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the error "unrecognized selector sent to instance" mean in Xcode
提问by Wasim
What does it mean "unrecognized selector sent to instance"
in Xcode
?
这是什么意思"unrecognized selector sent to instance"
的Xcode
?
回答by Flami
It means, method is not defined or on the other-way, calling a method on the wrong object.
这意味着,方法未定义,或者相反,在错误的对象上调用方法。
classic example of this error is missing of ':' in selector call.
此错误的经典示例在选择器调用中缺少“:”。
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self
action:@selector(DatePickerDoneClick)];
Here,
这里,
action:@selector(DatePickerDoneClick:)
is expected rather than
预期而不是
action:@selector(DatePickerDoneClick)
回答by OfficerDave
If you've deleted and re-created buttons on the Storyboard, you may have the button linked to two different methods: the old (deleted) one, and the new one. This has happened to me many times.
如果您在 Storyboard 上删除并重新创建了按钮,您可能会将按钮链接到两种不同的方法:旧的(删除的)方法和新的方法。这在我身上发生过很多次。
To fix: 1) In XCode/IB, view the Storyboard. 2) Click on the button that is causing the exception. 3) On the far-right panel, click the 'connector' icon. (A circle with an arrow in it, as of Apr-2015). 4) Assuming you're linking to the action "Touch Up Inside", make sure only ONE method is linked. An [x] will appear next to each, so if there are two, kill the one that no longer exists.
修复: 1) 在 XCode/IB 中,查看 Storyboard。2) 单击导致异常的按钮。3) 在最右侧的面板上,单击“连接器”图标。(一个带有箭头的圆圈,截至 2015 年 4 月)。4) 假设您要链接到“Touch Up Inside”操作,请确保只链接了一种方法。每个旁边都会出现一个 [x],因此如果有两个,请杀死不再存在的那个。
回答by Dean Wiley
In my case it means I did not understand (for two days) a very simple requirement of the handler (selector, function): I had left off the ...:(NSNotification*)notification... in my selector (function).
在我的情况下,这意味着我没有理解(两天)处理程序(选择器,函数)的一个非常简单的要求:我已经离开了... :(NSNotification*)notification... 在我的选择器(函数)中.
In the end it is just a self.stupidMistake (or programming tired while trying to understand a new thing in iOs/xCode). I read the docs at apple, I read many, many here at stackoverflow and read all kinds of other pages from the search results and just kept overlooking the fact that I had: in the viewDidLoad:
最后它只是一个 self.stupid 错误(或者在试图理解 iOs/xCode 中的新事物时编程累了)。我在 Apple 上阅读了文档,在 stackoverflow 上阅读了很多很多,并从搜索结果中阅读了各种其他页面,只是一直忽略了以下事实:在 viewDidLoad 中:
[[NSNotificationCenter defaultCenter] addOberserver:self selector:@selector(myHandler:) name:@"UIApplicationWillResignActiveNotification" object:nil];
in the .h (declaration) and .m (real code) I had invented:
在我发明的 .h(声明)和 .m(真实代码)中:
-(void)myHandler { ... }
This generated the unrecognized selector sent to instance(crash and debug output) at runtime (no errors or warnings in xcode). Then I spent almost two whole days trying to figure out the error and the error was:
这生成了在运行时发送到实例(崩溃和调试输出)的无法识别的选择器(xcode 中没有错误或警告)。然后我花了将近两天的时间试图找出错误,错误是:
-(void)myHandler:(NSNotification*)notification { ... }
Hope it helps anyone else stuck - it is a syntax thing (your Selector or Handler or Function or whatever you want to call it) musttake a (NSNotification*) 'object' as a parameter whether you use it or not; and xcode (4.2 w/iOs SDK 5.0) does not generate any errors or warnings about this 'mistake'.
希望它可以帮助其他人卡住 - 无论您是否使用它,它都是一个语法问题(您的选择器或处理程序或函数或任何您想调用它的东西)必须将 (NSNotification*) 'object' 作为参数;并且 xcode (4.2 w/iOs SDK 5.0) 不会产生关于这个“错误”的任何错误或警告。
回答by Nick Moore
It means that you have called a method on an object which does not support that method.
这意味着您在不支持该方法的对象上调用了一个方法。
The reason it says 'unrecognised selector' is that method invocation is implemented by a message sending mechanism. The part of the message that contains the method name is called the selector.
它说“无法识别的选择器”的原因是方法调用是由消息发送机制实现的。消息中包含方法名称的部分称为选择器。
回答by PgmFreek
I think this error is due to calling a function in class which is not declared in the class.
我认为这个错误是由于在类中调用了一个没有在类中声明的函数。
回答by Troydm
I think it's when you call a selector on instance of method that doesn't belong to this instance. maybe I'm wrong I'm not sure, it's been a while since I've coded anything in xcode.
我认为这是当您在不属于此实例的方法实例上调用选择器时。也许我错了我不确定,我已经有一段时间没有用 xcode 编写任何代码了。
回答by Gumflush
For me it was just Xcode
not bothering to compile the code. Apple obviously doesn't think iOS development is complicated enough, so they have to build extra flakiness into the IDE.
对我来说Xcode
,编译代码并不费心。Apple 显然认为 iOS 开发不够复杂,因此他们必须在 IDE 中构建额外的脆弱性。
I am told that SHIFT+COMMAND+K
to clean the build can be a great help.
有人告诉我,SHIFT+COMMAND+K
清理构建会很有帮助。
回答by Rico
I received this error due to not assigning the custom class to the view in the Interface Builder.
由于未将自定义类分配给 Interface Builder 中的视图,我收到此错误。
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"EventsFilterView" owner:self options:nil];
self.filters = [subviewArray objectAtIndex:0];
The variable self.filters
was assigned to the EventFilterView
class but the actual view in the xib file was not.
该变量self.filters
已分配给EventFilterView
该类,但 xib 文件中的实际视图未分配给该类。
Hope this helps someone.
希望这可以帮助某人。
回答by Javier Calatrava Llavería
There are several reasons why this can occur:
发生这种情况的原因有多种:
ONE:The method is declared in the .h, but does not exist in the .m. The compiler does not complain, but during execution you face that crash.Please,Check this out:
ONE:该方法在.h 中声明,但在.m 中不存在。编译器没有抱怨,但在执行过程中你会遇到崩溃。请检查一下:
- The method is implemented in the m.
- The method signature is exactly the same.
- There is not a semi-colon at the end of the method signature.
- 该方法在m中实现。
- 方法签名完全相同。
- 方法签名末尾没有分号。
TWO:If you are calling a delegated method, check if this method is really implemented.
二:如果调用的是委托方法,请检查该方法是否真的实现了。
I use to have this error when I modify the signature, but I forgot to update the signature method on the implementation file .m
我以前修改签名的时候出现这个错误,但是忘记更新实现文件.m上的签名方法了
回答by Tulleb
I had a similar problem and the issue was my ".m" class containing the unknown selector wasn't in the "Build Phases / Compile Sources" list. I added it and everything was fixed.
我有一个类似的问题,问题是我的包含未知选择器的“.m”类不在“构建阶段/编译源”列表中。我添加了它,一切都修复了。