objective-c 如何解决“无法识别的选择器发送到实例”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/861626/
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
How to resolve 'unrecognized selector sent to instance'?
提问by 4thSpace
In the AppDelegate, I'm alloc'ing an instance defined in a static library. This instance has an NSString property set a "copy". When I access the string property on this instance, the app crashes with 'unrecognized selector sent to instance'. Xcode provides a code hint for the property, which means it is known in the calling app. The particular class is compiled into the static library target. What am I missing?
在 AppDelegate 中,我分配了一个在静态库中定义的实例。这个实例有一个 NSString 属性设置一个“副本”。当我访问此实例上的字符串属性时,应用程序崩溃并显示“无法识别的选择器发送到实例”。Xcode 为该属性提供了一个代码提示,这意味着它在调用应用程序中是已知的。特定类被编译到静态库目标中。我错过了什么?
Adding some code.
添加一些代码。
//static library
//ClassA.h
@interface ClassA : NSObject {
...
NSString *downloadUrl;
}
@property(nonatomic, copy) NSString *downloadUrl;
//ClassA.m
@synthesize downloadUrl;
In the calling app's appDelegate.
在调用应用程序的 appDelegate 中。
//app delegate header file
@interface myApp : NSObject <UIApplicationDelegate> {
ClassA *classA;
}
@property (nonatomic, retain) ClassA *classA;
//app delegate .m file
@synthesize classA;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
classA = [[ClassA alloc] init];
//exception occurs here. downloadUrl is of type NSCFNumber
classA.downloadUrl = @"http://www.abc.com/";
...}
Other classes in the app will get a reference to the delegate and call classA.downloadUrl.
应用程序中的其他类将获得对委托的引用并调用 classA.downloadUrl。
采纳答案by stefanB
1) Is the synthesize within @implementationblock?
1) 是@implementation块内合成吗?
2) Should you refer to self.classA = [[ClassA alloc] init];and self.classA.downloadUrl = @"..."instead of plain classA?
2)你应该参考self.classA = [[ClassA alloc] init];andself.classA.downloadUrl = @"..."而不是 plain classA?
3) In your myApp.mfile you need to import ClassA.h, when it's missing it will default to a number, or pointer? (in C variables default to int if not found by compiler):
3)在您的myApp.m文件中,您需要导入ClassA.h,当它丢失时,它将默认为一个数字或指针?(在 C 变量中,如果编译器未找到,则默认为 int):
#import "ClassA.h".
#import "ClassA.h".
回答by Mihir Mehta
Set flag -ObjCin Other linker Flag in your Project setting... (Not in the static library project but the project you that is using static library...) And make sure that in Project setting Configuration is set to All Configuration
在项目设置中的其他链接器标志中设置标志-ObjC...(不在静态库项目中,而是在您使用静态库的项目中...)并确保在项目设置中将配置设置为所有配置
回答by Dave Kanter
A lot of people have given some very technical answers for this and similar questions, but I think it's simpler than that. Sometimes if you're not paying attention a selector that you don't intend to use can be attached to something in the interface. You might be getting this error because the selector's there but you haven't written any code for it.
很多人对这个和类似的问题给出了一些非常技术性的答案,但我认为它比那更简单。有时,如果您不注意,您不打算使用的选择器可能会附加到界面中的某些内容。您可能会收到此错误,因为选择器在那里,但您尚未为其编写任何代码。
The easiest way to double-check that this is not the case is to control-click the item so you can see all of the selectors that are associated with it. If there's anything in there that you don't want to be, get rid of it! Hope this helps...
仔细检查是否不是这种情况的最简单方法是按住 Control 单击该项目,以便您可以看到与其关联的所有选择器。如果里面有你不想成为的东西,那就摆脱它吧!希望这可以帮助...
回答by Rich
For me, what caused this error was that I accidentally had the same message being sent twice to the same class member. When I right clicked on the button in the gui, I could see the method name twice, and I just deleted one. Newbie mistake in my case for sure, but wanted to get it out there for other newbies to consider.
对我来说,导致这个错误的原因是我不小心将相同的消息发送给同一个班级成员两次。当我右键单击 gui 中的按钮时,我可以看到两次方法名称,而我只是删除了一个。在我的情况下肯定是新手错误,但想把它拿出来供其他新手考虑。
回答by Dan Power
Mine was something simple/stupid. Newbie mistake, for anyone that has converted their NSManagedObject to a normal NSObject.
我的东西很简单/愚蠢。新手错误,对于任何将他们的 NSManagedObject 转换为普通 NSObject 的人。
I had:
我有:
@dynamic order_id;
when i should have had:
当我应该有:
@synthesize order_id;
回答by Chuck
In the code you posted, you're sending the setDownloadURL:setter to ClassA— that is, the class itself. You want to set the property of an instance.
在您发布的代码中,您将setDownloadURL:setter发送到ClassA- 即类本身。您想设置实例的属性。
回答by Heat Miser
How are you importing ClassA into your AppDelegate Class? Did you include the .h file in the main project? I had this problem for a while because I didn't copy the header file into the main project as well as the normal #include "ClassA.h."
你是如何将 ClassA 导入你的 AppDelegate 类的?您是否在主项目中包含了 .h 文件?我有一段时间有这个问题,因为我没有将头文件复制到主项目以及正常的#include "ClassA.h"。
Copying, or creating the .h solved it for me.
复制或创建 .h 为我解决了这个问题。
回答by Peter N Lewis
You should note that this is not necessarily the best design pattern. From the looks of it, you are essentially using your App Delegate to store what amounts to a global variable.
您应该注意,这不一定是最好的设计模式。从它的外观来看,您实际上是在使用您的 App Delegate 来存储相当于全局变量的内容。
Matt Gallagher covered the issue of globals well in his Cocoa with Love article at http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html. In all likelyhood, your ClassA should be a singleton rather than a global in the AppDelegate, although its possible you intent ClassA to be more general purpose and not simply a singleton. In that case, you'd probably be better off with either a class method to return a pre-configured instance of Class A, something like:
Matt Gallagher 在他的 Cocoa with Love 文章(http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html )中很好地讨论了全局变量的问题。在所有可能的情况下,您的 ClassA 应该是一个单例,而不是 AppDelegate 中的全局对象,尽管您可能希望 ClassA 更通用,而不仅仅是一个单例。在这种情况下,使用类方法返回 A 类的预配置实例可能会更好,例如:
+ (ClassA*) applicationClassA
{
static ClassA* appClassA = nil;
if ( !appClassA ) {
appClassA = [[ClassA alloc] init];
appClassA.downloadURL = @"http://www.abc.com/";
}
return appClassA;
}
Or alternatively (since that would add application specific stuff to what is possibly a general purpose class), create a new class whose sole purpose is to contain that class method.
或者(因为这会将特定于应用程序的内容添加到可能是通用目的的类中),创建一个新类,其唯一目的是包含该类方法。
The point is that application globals do not need to be part of the AppDelegate. Just because the AppDelegate is a known singleton, does not mean every other app global should be mixed in with it even if they have nothing conceptually to do with handling the NSApplication delegate methods.
关键是应用程序全局变量不需要成为 AppDelegate 的一部分。仅仅因为 AppDelegate 是一个已知的单例,并不意味着所有其他应用程序全局都应该与它混合,即使它们在概念上与处理 NSApplication 委托方法没有任何关系。
回答by H2CO3
Very weird, but. You have to declare the class for your application instance as myApplication: UIApplication instead of myApplication: NSObject . It seems that the UIApplicationDelegate protocol doesn't implement the +registerForSystemEvents message. Crazy Apple APIs, again.
很奇怪,但是。您必须将应用程序实例的类声明为 myApplication: UIApplication 而不是 myApplication: NSObject 。似乎 UIApplicationDelegate 协议没有实现 +registerForSystemEvents 消息。又是疯狂的 Apple API。

