Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c obj-c,困惑为什么可以';我不能给这个类添加一个setter吗?_Objective C_Xcode - Fatal编程技术网

Objective c obj-c,困惑为什么可以';我不能给这个类添加一个setter吗?

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

通常,向类添加setter方法不会有任何问题

但是,我正在尝试添加到library usage类,这一定是导致问题的原因

这是我添加到的类

@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];