Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 混乱的方法-if/then结构和字符串操作问题_Iphone_Xcode_Nsstring_Logic_If Statement - Fatal编程技术网

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

Iphone 混乱的方法-if/then结构和字符串操作问题,iphone,xcode,nsstring,logic,if-statement,Iphone,Xcode,Nsstring,Logic,If Statement,我有一个方法,每当视图上的控件发生更改时都会被调用,并且应该更新UILabel。它有两个UITextFields和两个UISliders。首先,我检查UITextFields是否为空,如果为空,建议需要填写它们。否则,我将获得UITextFields'值之间的差异,并生成两个floats以在我的NSStrings中使用 我收到一个警告,提示未使用消息,并且我收到一个有关NSString的错误(现在记不清确切的内容-我不在Mac上…) 甚至当我将消息分解为简单的有效消息时,当delta==0时,它

我有一个方法,每当视图上的控件发生更改时都会被调用,并且应该更新
UILabel
。它有两个
UITextField
s和两个
UISlider
s。首先,我检查
UITextField
s是否为空,如果为空,建议需要填写它们。否则,我将获得
UITextField
s'值之间的差异,并生成两个
float
s以在我的
NSString
s中使用

我收到一个警告,提示未使用
消息
,并且我收到一个有关
NSString
的错误(现在记不清确切的内容-我不在Mac上…)

甚至当我将消息分解为简单的有效消息时,当
delta==0
时,它也会执行
delta=0){
NSString*消息=[[NSString alloc]initWithFormat:@“若要将FC增加%dppm,请添加%3.1f盎司%@.”,增量、氯含量、氯源字段.text];
}

if(delta对于
消息
变量而言,它的作用域是有限的,因此不能在声明它的
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];
}
-(无效)更新设备{
NSString*消息=nil;
if([chlorenesourcefield.text IsequalString:@”“]| |[poolVolumeField.text IsequalString:@”“){
message=[[NSString alloc]initWithString:@“输入氯源和池体积。”];
}
否则{
int delta=[targetLabel.text intValue]-[startingLabel.text intValue];
浮点数氯离子含量=δ*[poolVolumeField.text intValue]*氯离子常数;
浮动百分比移除=(1-([targetLabel.text floatValue]/[startingLabel.text floatValue]);
float gallonsRemove=percentRemove*[poolVolumeField.text intValue];
如果(增量==0){
message=[[NSString alloc]initWithString:@“无需调整。您已达到目标”];
}
如果(增量>=0){
message=[[NSString alloc]initWithFormat:@“要将FC增加%dppm,请添加%3.1f盎司的%@.”,delta,chloreneamount,chlorenesourcefield.text];
}

如果(delta您缺少
else
部分,那么所有三条
if
语句都将被连续计算。如果
delta==0
,它将满足所有三条
if
语句。因此,最后一次分配将覆盖前两条语句。(并且您将泄漏内存)

此外,如果将
消息
声明到
块中,则将
消息
变量的作用域设置为
块的本地。您可能希望将
消息
声明移动到函数级作用域


如果
%
不起作用,则使用实例初始值设定项和类初始值设定项的语法。
initWithFormat
在单独的参数中获取格式化字符串的参数-
参数:
(顺便说一句,它还需要
区域设置:
)Duh,我不想要小于或等于,只想要小于。我是C世界的新手。我可以在条件中使用
吗?等于的
=
会把我甩了=)你知道为什么%d部分不起作用吗?是的,你可以只使用
。我仍然会将
其他部分
添加到
if
块中-你不需要总是对这三个部分进行求值。不,我不知道为什么%d部分不起作用。实际上,我刚刚找到了%d部分不起作用的原因。我会更新我的答案。我会相信苹果的文档比任何一本书都要准确。:-)但如果它工作得很好,那就是你的决定。从你的帖子和评论来看,我的印象是它实际上不起作用。当我说它起作用时,我的意思是我评论中的片段在书中的示例程序的上下文中起作用-你是对的,我的代码不起作用。我查阅了苹果的文档,看到了你的意思是什么。他们用
stringWithFormat:
来做我的字符串。我今晚会在家里试试看。再次感谢患者的帮助。谢谢!如果没有帮助,我永远不会明白这一点!+1
- (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];
}