我可能错过了一些明显的东西(仍在学习Objective-C!),但由于某种原因,我的一个NSString变量在我的if
声明中有一个空值,我不知道为什么?iOS:NSString变量返回null
我甚至输出到NSLog,我仍然不明白为什么它的行为如此。
基本上,用户在文本字段中输入金额(itemWeight),并且此if
语句验证输入并根据结果显示警报。这个问题似乎只是当输入0.751时,如果你输入任何其他数量(0.750,0.749,0.752,0.753等),它按预期工作。
相关代码示例如下...
.h文件中:
@property (strong, nonatomic) IBOutlet UITextField *itemWeight;
.m文件:
NSString *rawWeightText = itemWeight.text;
float convertedWeightText = rawWeightText.floatValue;
NSString *weightMessage;
if (convertedWeightText <= 0.750)
{
weightMessage = @"under 0.750";
}
else if (convertedWeightText >= 0.751)
{
weightMessage = @"0.751 or over";
}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Error"
message: weightMessage
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
任何想法,我要去哪里错了/我”已经忘记做会非常感激,谢谢!
'NSLog(@“%f”,convertedWeightText)'的输出是什么?你知道的事实是,当我输入一个介于0.750和0.751之间的值时,你的'if-elseif'不匹配? – mAu 2012-08-11 14:48:54
NSLog输出是0.751000 – 2012-08-11 14:52:49
是的,但是这是四舍五入的。 'NSLog(@“%。10f”,convertedWeightText);'给你'0.7509999871' – 2012-08-11 15:02:19