-1

我正在尝试比较从两种不同方法获得的数据:第一个来自实例方法,第二个来自类方法。我收到一个难以理解的警告和一个错误。

这是界面:

@interface RadioStation : NSObject {

NSString *name;
double frequency;
char band;

}
+(double) maxFMFrequency;
-(void) chackFrequency;

@end

这是实现:

@implementation RadioStation

+(double) maxFMFrequency {

return 107.9;
}
-(void) chackFrequency {

    switch (band) {
        case 'F':
            if (self.frequency > [[self RadioStation] maxFMFrequency] ) //  in this line i get the warning and the error massage
                frequency=107.9;
            break;


@end

这是我得到的警告:

instance method '-RadioStation' not found (return type defaults to 'id')

当我构建并运行程序时,我得到了错误

Thread 1: signal SIGABRT

有谁知道我做错了什么?

谢谢!

4

3 回答 3

3

应该读:

if (self.frequency > [RadioStation maxFMFrequency] )

如果按名称寻址,则不需要 Self 来寻址类。如果你想引用 self 你可以使用:

if (self.frequency > [[self class] maxFMFrequency] )
于 2012-04-08T18:59:27.167 回答
1

从你的帖子中不清楚波段和频率的值来自哪里,所以我只是将它们放在 init 方法中进行测试,当我在这个类上调用 alloc init 时,这段代码运行良好:

@implementation RadioStation

-(id)init {
    if (self = [super init]) {
        band = 'F';
        frequency = 120;
        [self chackFrequency];
        return self;
    }else{
        return nil;
    }
}

+(double) maxFMFrequency {
    return 107.9;
}

-(void) chackFrequency {
    switch (band) {
        case 'F':
            if (frequency > [[self class] maxFMFrequency] )
                frequency=107.9;
            NSLog(@"%f",frequency);
            break;
    }
}
@end
于 2012-04-08T21:57:51.617 回答
0

也许你的错误如下:

[[self RadioStation] maxFMFrequency]

改变它

[RadioStation maxFMFrequency]
于 2012-04-08T19:01:45.490 回答