开发者

obj-c, confusion why can't I add a setter to this class?

Normally I don't have any problem adding a setter method to a class.

However, I'm trying to add to library usage class, which must be causing the problem.

Heres the class I've added it to...

@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

i开发者_Go百科f (value != strChartType) {
    [value retain];
    [strChartType release];
    strChartType = value;
    NSLog(@"ChartType=%@", strChartType);
}     
}

Heres where I'm getting a warning..

UIViewController *vc = [[GraphController alloc] init];      
[vc setContentType:myGraphType];  //Warnings on this line see below
[self presentModalViewController:vc animated:NO];
[vc release];

myGraphType if defined in my constant class.

* Warnings *

warning: 'UIViewController' may not respond to '-setContentType:'
warning: (Messages without a matching method signature

I know the first warning appears when you haven't added the method to the implementation. but I have.

Where am I going wrong ?


UIViewController *vc = [[GraphController alloc] init];

means that vc points to an instance of GraphController but the variable itself is of type UIViewController *, and UIViewController doesn’t declare a -setContentType: method.

Replace that with

GraphController *vc = [[GraphController alloc] init];

to tell the compiler you’re working with a GraphController instance, and it will recognise your -setContentType: method.


You just have to let the compiler know that you're working with a class that it knows responds to the method. You can do it a couple of ways but the easiest one if you just want to eliminate the warning is to cast the object in line before you make the method call.

UIViewController *vc = [[GraphController alloc] init];      
[(GraphController *)vc setContentType:myGraphType];  //No warning should appear now.
[self presentModalViewController:vc animated:NO];
[vc release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜