Objective c obj-c,困惑为什么可以';我不能给这个类添加一个setter吗?
通常,向类添加setter方法不会有任何问题 但是,我正在尝试添加到library usage类,这一定是导致问题的原因 这是我添加到的类Objective c obj-c,困惑为什么可以';我不能给这个类添加一个setter吗?,objective-c,xcode,Objective C,Xcode,通常,向类添加setter方法不会有任何问题 但是,我正在尝试添加到library usage类,这一定是导致问题的原因 这是我添加到的类 @interface GraphController : TKGraphController { UIActivityIndicatorView *indicator; NSMutableArray *data; //I've added NSString *strChartType; //I've added } -(void)setContentType
@interface GraphController : TKGraphController {
UIActivityIndicatorView *indicator;
NSMutableArray *data; //I've added
NSString *strChartType; //I've added
}
-(void)setContentType:(NSString*)value; //I've added
@end
@implementation GraphController
-(void)setContentType:(NSString*)value { //I've added
if (value != strChartType) {
[value retain];
[strChartType release];
strChartType = value;
NSLog(@"ChartType=%@", strChartType);
}
}
这是我得到警告的地方
UIViewController *vc = [[GraphController alloc] init];
[vc setContentType:myGraphType]; //Warnings on this line see below
[self presentModalViewController:vc animated:NO];
[vc release];
如果在常量类中定义了myGraphType
*警告*
warning: 'UIViewController' may not respond to '-setContentType:'
warning: (Messages without a matching method signature
我知道当您尚未将方法添加到实现中时,会出现第一个警告。但我有
我哪里做错了
UIViewController *vc = [[GraphController alloc] init];
表示vc
指向GraphController
的实例,但变量本身的类型为UIViewController*
,并且UIViewController
不声明-setContentType:
方法
换成
GraphController *vc = [[GraphController alloc] init];
告诉编译器您正在使用GraphController
实例,它将识别您的-setContentType:
方法
表示vc
指向GraphController
的实例,但变量本身的类型为UIViewController*
,并且UIViewController
不声明-setContentType:
方法
换成
GraphController *vc = [[GraphController alloc] init];
要告诉编译器您正在使用一个
GraphController
实例,它将识别您的-setContentType:
方法。您只需让编译器知道您正在使用一个它知道响应该方法的类。您可以通过两种方法来实现,但是如果您只是想消除警告,最简单的方法是在进行方法调用之前将对象强制转换成一行
UIViewController *vc = [[GraphController alloc] init];
[(GraphController *)vc setContentType:myGraphType]; //No warning should appear now.
[self presentModalViewController:vc animated:NO];
[vc release];
您只需要让编译器知道您正在使用一个类,它知道该类响应该方法。您可以通过两种方法来实现,但是如果您只是想消除警告,最简单的方法是在进行方法调用之前将对象强制转换成一行
UIViewController *vc = [[GraphController alloc] init];
[(GraphController *)vc setContentType:myGraphType]; //No warning should appear now.
[self presentModalViewController:vc animated:NO];
[vc release];