2013-09-16 51 views
-4

我試圖從這個問題再現例如:Objective-C的實現和調用方法

How to create a method in Objective-C that takes a NSString stringWithFormat as a parameter?

而且我在@interface創建:

- (float)strToFloat:(NSString *)str; 

而且在@implementation :

- (float)strToFloat:(NSString *)str { 

    str = [str stringByReplacingOccurrencesOfString:@"." 
             withString:@""]; 
    str = [str stringByReplacingOccurrencesOfString:@"," 
             withString:@"."]; 
    return [str floatValue]; 
} 

畢竟,當我嘗試編譯應用程序,我收到這個錯誤:Use of undeclared identifier 'strToFloat'用這種方法:

- (IBAction)addItem:(id)sender { 
    NSLog(@"float value is: %.2f", [strToFloat labelPrice.text]); 
} 

這個例子有什麼問題?

+5

你應該回去閱讀一個基本的Objective-C教程。 – 2013-09-16 18:25:02

+0

我還在讀書,在試圖提出這個問題之前,我單獨失去了幾個小時,現在這個問題已經非常明顯 - 但是以新語言開始的人會遇到同樣的困難 –

回答

6

你必須調用正確的方法:

NSLog(@"float value is: %.2f", [self strToFloat:labelPrice.text]); 

我假設你的addItem:strToFloat:方法是在同一個班級。

順便說一句 - 有很多更好的方法來將十進制字符串值轉換爲處理用戶區域設置的數字。利用NSNumberFormatter

+0

就是這樣。我是新的Objective-C,這個語法對我來說意義非常明顯,謝謝! –