2012-04-08 36 views
-1

我想比較,我從兩種不同的方法得到的數據:從一個實例方法的第一個,並從一個類方法的第二個。我得到一個難以理解的警告和一個錯誤。問題Objective-C的方法比較

這是接口:

@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

有誰知道我做錯了嗎?

謝謝!

回答

3

應改爲:不需要

if (self.frequency > [RadioStation maxFMFrequency]) 

自我解決類,如果按名稱尋址。如果你想引用自我,你可以使用:

if (self.frequency > [[self class] maxFMFrequency]) 
+0

當我寫這個我只得到錯誤按摩 – 2012-04-08 19:09:08

+0

@shaynagar在你發佈你的「'chackFrequency'」方法的代碼沒有閉合大括號。如果你的實際代碼是這樣的,那麼這將是一個問題。 – NJones 2012-04-08 19:14:59

0

也許你的錯誤如下:

[[self RadioStation] maxFMFrequency] 

[RadioStation maxFMFrequency] 
+0

當我寫這我只得到了錯誤的按摩 – 2012-04-08 19:09:24

+0

錯誤可能是使用頻度而不使您的if語句 – WhiteTiger 2012-04-08 19:22:56

+0

self.frequency你打電話,但實際上您尚未創建此變量 – WhiteTiger 2012-04-08 19:28:05

1

改變它,它是不會從你的帖子裏明確對於波段和頻率來自於價值觀,所以我只是堅持他們用於測試目的init方法,這個代碼工作得很好,當我呼籲這個類的alloc初始化:

@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 
+0

THX,其現在的工作的性質首先初始化 – 2012-04-09 08:40:33

+1

@shaynagar如果rdelmar的回答回答了您的問題,您應該將其標記爲已接受的答案。 – NJones 2012-04-10 03:48:31