如何解决“无法识别的选择器发送到实例”? [英] How to resolve 'unrecognized selector sent to instance'?

查看:197
本文介绍了如何解决“无法识别的选择器发送到实例”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AppDelegate中,我分配了一个在静态库中定义的实例。这个实例有一个NSString属性设置一个复制。当我访问此实例上的字符串属性时,应用程序崩溃,并显示无法识别的选择器发送到实例。 Xcode为属性提供了一个代码提示,这意味着它在调用应用程序中是已知的。特定的类被编译成静态库目标。我缺少什么?

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?

添加一些代码。

//static library 
//ClassA.h
@interface ClassA : NSObject {
...
NSString *downloadUrl;
}
@property(nonatomic, copy) NSString *downloadUrl;

//ClassA.m
@synthesize downloadUrl;

在致电应用程式的appDelegate中。

In the calling app's 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/";
...}

应用程序中的其他类将获得对委托的引用,调用classA.downloadUrl。

Other classes in the app will get a reference to the delegate and call classA.downloadUrl.

推荐答案

1)是否在 @implementation block?

1) Is the synthesize within @implementation block?

2)你应该参考 self.classA = [[ClassA alloc] init]; self.classA.downloadUrl = @...而不是纯粹 classA

2) Should you refer to self.classA = [[ClassA alloc] init]; and self.classA.downloadUrl = @"..." instead of plain classA?

3)在 myApp.m 文件中,您需要导入 ClassA.h 当它缺少它会默认一个数字,或指针? (在C变量中默认为int,如果编译器没有找到):

3) In your myApp.m file 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):

#importClassA.h

这篇关于如何解决“无法识别的选择器发送到实例”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆