Ios 使用if函数设置NSString的值
在尝试将我的Android应用程序转换为iOS应用程序后,遇到了一些问题: 这是Java代码的一个示例块:Ios 使用if函数设置NSString的值,ios,objective-c,if-statement,nsstring,Ios,Objective C,If Statement,Nsstring,在尝试将我的Android应用程序转换为iOS应用程序后,遇到了一些问题: 这是Java代码的一个示例块: String temperature; int x,y; if (x > y){ temperature = "Increased"; } else { temperature = "Decreased"; } System.out.println (temperature); 现在假设我想用Objective-C写这篇文章,我目前正在做的是: NSString *tempe
String temperature;
int x,y;
if (x > y){
temperature = "Increased";
}
else {
temperature = "Decreased";
}
System.out.println (temperature);
现在假设我想用Objective-C写这篇文章,我目前正在做的是:
NSString *temperature = @"Something is not working";
int x,y;
if (x > y){
temperature = "Increased";
}
else {
temperature = "Decreased";
}
我做错了什么?如果这是显而易见的,我道歉。我尝试过搜索这个,但显然,我问了一个错误的问题(尝试过搜索NSString、NSMutableString、如何使用if函数覆盖NSString等) 您的代码还可以,只需稍加修改即可。在字符串之前使用
@
符号
NSString *temperature = @"Something is not working";
int x,y;
if (x > y){
temperature = @"Increased";
}
else {
temperature = @"Decreased";
}
更新
您不需要初始化temperature
变量,因为您有else
部分,这意味着temperature
变量将始终被新值覆盖
NSString *temperature;
int x,y;
if (x > y){
temperature = @"Increased";
}
else {
temperature = @"Decreased";
}
你的代码很好,只需要稍加修改。在字符串之前使用
@
符号
NSString *temperature = @"Something is not working";
int x,y;
if (x > y){
temperature = @"Increased";
}
else {
temperature = @"Decreased";
}
更新
您不需要初始化temperature
变量,因为您有else
部分,这意味着temperature
变量将始终被新值覆盖
NSString *temperature;
int x,y;
if (x > y){
temperature = @"Increased";
}
else {
temperature = @"Decreased";
}
您的代码有两个错误:(1)在
NSString
之前有-
,不确定为什么有这个,但它不是必需的;(2)在创建像您这样的字符串时,最常见的错误是为什么要使用像@“some string”这样的字符串文字
请注意,字符串的前缀是@
字符
因此,让我们清理您的代码
// Not going to preset this because that value would never actually get used.
NSString *temperature; // Notice no "-"
int x, y;
if (x > y) {
// Notice the String literal prefixed with @
temperature = @"Increased";
} else {
// Notice the String literal prefixed with @
temperature = @"Decreased";
}
// NSLog is the equivalent to System.out.println
NSLog(@"The temperature has %@", temperature);
请注意,在您的代码和已清理的代码中,
if语句的else部分始终会到达,因为x
和y
是相同的。您的代码有两个错误(1)由于在NSString
之前有-
,所以不确定为什么会有这个字符串,但它不是必需的;(2)当创建像您这样的字符串时,最常见的是为什么要使用字符串文字,如@“some string”
请注意,字符串前面有@
字符
因此,让我们清理您的代码
// Not going to preset this because that value would never actually get used.
NSString *temperature; // Notice no "-"
int x, y;
if (x > y) {
// Notice the String literal prefixed with @
temperature = @"Increased";
} else {
// Notice the String literal prefixed with @
temperature = @"Decreased";
}
// NSLog is the equivalent to System.out.println
NSLog(@"The temperature has %@", temperature);
请注意,在您的代码和清理后的代码中,if语句的else部分始终会到达,因为x
和y
是相同的。在字符串前面加上@,表示它是NSString
而不是C字符串-temperature=@“增加”代码>此外,当您像“某些东西不工作”一样分配温度时,您不需要设置温度值。您可以从[[NSString alloc]init]开始,如果您在分配时使用[[NSString alloc]initWithFormat:@“…”,则可能不需要它。在字符串前面加@表示它是NSString
而不是C字符串-温度=@“增加”代码>此外,当您像“某些东西不工作”一样分配温度时,您不需要设置温度值。你可以从[[NSString alloc]init]开始,如果你在赋值时使用[[NSString alloc]initWithFormat:@“…”,这可能也不需要。我完全不同意这个答案中的一句话,那就是“你的代码正常”
,因为它不正常:)你只需从一个意义完全不同的句子中选取几个单词。“需要稍加修改”怎么样?让我们看一下整句话“你的代码很好,只需要稍加修改”
。嗯,我仍然不同意,因为“您的代码是正常的”
即使有结尾,您仍然在告诉OP他们的代码是正常的,而不是正常的<代码>“只需要一点修改”
说它只需要一些改进,但这就是使代码正常的原因。不好,显然不好。此“您的代码正常,只需稍加修改。”
会使您的答案不正确,因为“您的代码正常”
有那么一点点。它应该是“你的代码只需要稍微修改一下就可以了”
。这个句子也自相矛盾,说它可以,然后说它需要稍微修改。如果它需要一点修改,这是不好的。当有人张贴的东西,这意味着它不是100%正确,他们面临着一些问题,这就是为什么这里有。同样,他做得很好,只是不幸错过了@
标志。。。那么,如果我说你的代码没问题,你有什么问题。因为他在技术上是正确的。我完全不同意这个答案中的一句话,那就是“你的代码是正确的”
,因为它是不正确的:)你只是从一个意义完全不同的句子中挑选几个词。“需要稍加修改”怎么样?让我们看一下整句话“你的代码很好,只需要稍加修改”
。嗯,我仍然不同意,因为“您的代码是正常的”
即使有结尾,您仍然在告诉OP他们的代码是正常的,而不是正常的<代码>“只需要一点修改”
说它只需要一些改进,但这就是使代码正常的原因。不好,显然不好。此“您的代码正常,只需稍加修改。”
会使您的答案不正确,因为“您的代码正常”
有那么一点点。它应该是“你的代码只需要稍微修改一下就可以了”
。这个句子也自相矛盾,说它可以,然后说它需要稍微修改。如果它需要一点修改,这是不好的。当有人张贴的东西,这意味着它不是100%正确,他们面临着一些问题,这就是为什么这里有。同样,他做得很好,只是不幸错过了@
标志。。。那么,如果我说你的代码没问题,你有什么问题。因为他从技术上讲是正确的。非常感谢你,大力水手。非常清晰明了。=)关于我不太明白的事情的快速问题:这行:NSLog(@“温度有%@”,温度);%@的函数是什么?如果我想在NSLog中调用多个字符串,我将如何应用它?我能不能