混乱的方法 - if/then 结构和字符串操作问题
我有一个方法,只要我的视图上的控件发生更改,就会调用该方法,并且应该更新 UILabel
。它有两个 UITextField
和两个 UISlider
。首先,我检查是否有一个 UITextField
为空,如果是,则建议需要填写它们。否则我会得到 UITextField
之间的差异值并生成几个 float
以便在我的 NSString
中使用。
我收到一条警告,指出 message
未使用,并且收到有关 NSString
的错误(现在不记得具体是什么 - 我不在我的 Mac 旁边。) .)
即使我将消息简化为简单有效的内容,当 delta == 0 时,它也会执行 delta <= 0 消息。
哦,字符串不会将值放在 %
符号所在的位置,它们只是打印 %
符号。
我已经对此进行了太长时间的黑客攻击,需要帮助......
- (void)updateAdvice {
if ([chlorineSourceField.text isEqualToString:@""] || [poolVolumeField.text isEqualToString:@""]) {
NSString *message = [[NSString alloc] initWithString:@"Enter a chlorine source and pool volume."];
}
else {
int delta = [targetLabel.text intValue] - [startingLabel.text intValue];
float chlorineAmount = delta * [poolVolumeField.text intValue] * chlorineConstant;
float percentRemove = (1 - ([targetLabel.text floatValue] / [startingLabel.text floatValue]));
float gallonsRemove = percentRemove * [poolVolumeField.text intValue];
if (delta == 0) {
NSString *message = [[NSString alloc] initWithString:@"No adjustments necessary. You're on target"];
}
if (delta >= 0) {
NSString *message = [[NSString alloc] initWithFormat:@"To increase FC by %dppm, add %3.1f oz of %@.", delta, chlorineAmount, chlorineSourceField.text];
}
if (delta <= 0) {
NSString *message = [[NSString alloc] initWithFormat:@"You're above target already. Replace %d%% or %d gallons of water - or just wait for it to come down.", percentRemove*100, gallonsRemove];
}
}
adviceLabel.text = message;
[message release];
}
I have a method that gets called any time a control on my view changes and should update a UILabel
. It has two UITextField
s and two UISlider
s. First I check to see if either of the UITextField
s are empty and if so, advise that they need to be filled in. Otherwise I get the difference between the UITextField
s' values and generate a couple of float
s to use in my NSString
s.
I get a warning that message
is not used and I get an error about the NSString
s (can't remember now exactly what - I'm not at my Mac...)
And even when I chopped the messages down to something simple that worked, when delta == 0
, it does the delta <= 0
message.
Oh, and the strings don't put the values in where the %
signs are, they just print the %
signs.
I've been hacking at this too long and need help...
- (void)updateAdvice {
if ([chlorineSourceField.text isEqualToString:@""] || [poolVolumeField.text isEqualToString:@""]) {
NSString *message = [[NSString alloc] initWithString:@"Enter a chlorine source and pool volume."];
}
else {
int delta = [targetLabel.text intValue] - [startingLabel.text intValue];
float chlorineAmount = delta * [poolVolumeField.text intValue] * chlorineConstant;
float percentRemove = (1 - ([targetLabel.text floatValue] / [startingLabel.text floatValue]));
float gallonsRemove = percentRemove * [poolVolumeField.text intValue];
if (delta == 0) {
NSString *message = [[NSString alloc] initWithString:@"No adjustments necessary. You're on target"];
}
if (delta >= 0) {
NSString *message = [[NSString alloc] initWithFormat:@"To increase FC by %dppm, add %3.1f oz of %@.", delta, chlorineAmount, chlorineSourceField.text];
}
if (delta <= 0) {
NSString *message = [[NSString alloc] initWithFormat:@"You're above target already. Replace %d%% or %d gallons of water - or just wait for it to come down.", percentRemove*100, gallonsRemove];
}
}
adviceLabel.text = message;
[message release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少
else
部分,因此所有三个if
语句都会被连续评估。如果delta == 0
,它将满足所有三个if
语句。因此,最后的分配将覆盖前两个。 (并且您将泄漏内存)此外,您的
message
变量的作用域是其声明的if
块的本地范围。您可能希望在函数级别范围内移动message
声明。至于
%
不起作用,您正在使用实例初始值设定项initWithFormat
具有类初始值设定项stringWithFormat
。initWithFormat
在单独的参数 -arguments:
中获取格式化字符串的参数。 (顺便说一句,它还需要locale:
)You are missing the
else
part, thus all threeif
statements are being evaluated consecutively. Ifdelta == 0
, it will satisfy all threeif
statements. Thus, the last allocation will overwrite the previous two. (And you'll be leaking memory)Also, your
message
variable is scoped as local to theif
block it's declared into. You might want to move themessage
declaration at the function level scope.As far as
%
not working, you are using the instance initializerinitWithFormat
with the syntax for the class initializerstringWithFormat
.initWithFormat
takes the parameters for the formatted string in a separate parameter -arguments:
. (Btw, it also requireslocale:
)就
message
变量而言,它是有作用域的,因此不能在声明它的if
/else
语句之外使用它。您希望在if
语句之前声明它,这样您就可以在if
语句之外使用它。所以像这样:As far as the
message
variable goes, it's scoped so it can't be used outside theif
/else
statement in which it is declared. You want to declare it before theif
statement, so you can use it outside of theif
statement. So something like this: