2012-04-07 82 views
2

我想從一個實例方法調用類的方法來使用它的返回值類方法。調用從一個實例方法

這是我的類方法

+(double) minFMFrequency { 

    return 88.3; 
} 

,這是我的實例方法

-(void) chackFrequency { 

    switch (band) { 
     case 'F': 
      if (self.frequency > Value obtained from class method) 
       frequency=107.9; 
      break; 

     default: 
      break; 
    } 

} 

bandfrequency是實例變量。

+1

所以,你想知道要放什麼東西來代替從類method'獲得'價值?你有什麼嘗試?的 – 2012-04-07 19:35:23

+0

可能重複的[目標c:調用同一類別內類方法](http://stackoverflow.com/questions/7290156/objective-c-call-class-method-within-same-class) – 2012-04-07 19:37:39

+0

我試圖自我。 minFMFrequency, [self minFMFrequency] – 2012-04-07 19:41:32

回答

0

Dupe。檢查this

你需要做的:[[self class] ClassProperties]

所以......你: 內chackFrequency你可以調用[[self class] minFMFrequency]

+0

當我嘗試這樣做時出現此錯誤:Thread1:信號SIGABRT – 2012-04-07 19:48:27

12
+(void)classMethod 
{ 
    [self anotherClassMethod]; // in a class method, self refers to the class 
} 

-(void)instanceMethod 
{ 
    [self anotherInstanceMethod]; //in an instance method self refers to the object, or instance 

    [[self class] classMethod]; //to call a class method from an instance send the instance the class message 

} 

所以你的情況:[[self class] minFMFrequency];

+0

當我嘗試這樣做時出現此錯誤:Thread1:信號SIGABRT – 2012-04-07 19:58:52

+1

但不是因爲此代碼。你應該用你的代碼和所有錯誤信息發佈一個新問題。 – vikingosegundo 2012-04-07 20:09:25

相關問題