混乱的方法 - if/then 结构和字符串操作问题

发布于 2024-09-11 08:01:53 字数 1841 浏览 3 评论 0原文

我有一个方法,只要我的视图上的控件发生更改,就会调用该方法,并且应该更新 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 UITextFields and two UISliders. First I check to see if either of the UITextFields are empty and if so, advise that they need to be filled in. Otherwise I get the difference between the UITextFields' values and generate a couple of floats to use in my NSStrings.

I get a warning that message is not used and I get an error about the NSStrings (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2024-09-18 08:01:53

您缺少 else 部分,因此所有三个 if 语句都会被连续评估。如果delta == 0,它将满足所有三个if语句。因此,最后的分配将覆盖前两个。 (并且您将泄漏内存)

此外,您的 message 变量的作用域是其声明的 if 块的本地范围。您可能希望在函数级别范围内移动 message 声明。

至于 % 不起作用,您正在使用实例初始值设定项 initWithFormat 具有类初始值设定项 stringWithFormatinitWithFormat 在单独的参数 - arguments: 中获取格式化字符串的参数。 (顺便说一句,它还需要 locale:

You are missing the else part, thus all three if statements are being evaluated consecutively. If delta == 0, it will satisfy all three if 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 the if block it's declared into. You might want to move the message declaration at the function level scope.

As far as % not working, you are using the instance initializer initWithFormat with the syntax for the class initializer stringWithFormat. initWithFormat takes the parameters for the formatted string in a separate parameter - arguments:. (Btw, it also requires locale:)

玩物 2024-09-18 08:01:53

message 变量而言,它是有作用域的,因此不能在声明它的 if/else 语句之外使用它。您希望在 if 语句之前声明它,这样您就可以在 if 语句之外使用它。所以像这样:

- (void)updateAdvice {
    NSString *message = nil;

    if ([chlorineSourceField.text isEqualToString:@""] || [poolVolumeField.text isEqualToString:@""]) {
        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) {
            message = [[NSString alloc] initWithString:@"No adjustments necessary.  You're on target"];
        }
        if (delta >= 0) {
            message = [[NSString alloc] initWithFormat:@"To increase FC by %dppm, add %3.1f oz of %@.", delta, chlorineAmount, chlorineSourceField.text];
        }
        if (delta <= 0) {
            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];
}

As far as the message variable goes, it's scoped so it can't be used outside the if/else statement in which it is declared. You want to declare it before the if statement, so you can use it outside of the if statement. So something like this:

- (void)updateAdvice {
    NSString *message = nil;

    if ([chlorineSourceField.text isEqualToString:@""] || [poolVolumeField.text isEqualToString:@""]) {
        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) {
            message = [[NSString alloc] initWithString:@"No adjustments necessary.  You're on target"];
        }
        if (delta >= 0) {
            message = [[NSString alloc] initWithFormat:@"To increase FC by %dppm, add %3.1f oz of %@.", delta, chlorineAmount, chlorineSourceField.text];
        }
        if (delta <= 0) {
            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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文