-4

我试图从这个问题中重现一个例子:

如何在 Objective-C 中创建一个以 NSString stringWithFormat 作为参数的方法?

我在@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]);
}

这个例子有什么问题?

4

1 回答 1

6

您必须正确调用该方法:

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

我假设你addItem:strToFloat:方法在同一个类中。

顺便说一句 - 有更好的方法可以将十进制字符串值转换为处理用户区域设置的数字。利用NSNumberFormatter.

于 2013-09-16T18:23:49.740 回答