ios - 我有两个 if 语句,第一个被跳过,为什么?

标签 ios if-statement

您好,我在使用多个 if 语句时遇到问题。这是我的代码:

if ([itemOnSpecial caseInsensitiveCompare: @"yes"] == NSOrderedSame) {
    UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
    specialLabel.text = specialPrice;
    [specialLabel setHidden:NO]; 
    }
//This statement is completely skipped

if ([isOnBulkSpecial caseInsensitiveCompare:@"yes"] == NSOrderedSame) {
            UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
            specialLabel.text = bulkSpecialPrice;
            [specialLabel setHidden:NO]; 

}else{
    UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
    [specialLabel setHidden:YES];
}

仅考虑第二个 if 语句。第一个 if 语句似乎被完全忽略了。

最佳答案

如果您将代码更改为:

if ([itemOnSpecial caseInsensitiveCompare: @"yes"] == NSOrderedSame) {
    UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
    specialLabel.text = specialPrice;
    [specialLabel setHidden:NO]; 
    }
else
{


   if ([isOnBulkSpecial caseInsensitiveCompare:@"yes"] == NSOrderedSame) {
        UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
            specialLabel.text = bulkSpecialPrice;
            [specialLabel setHidden:NO]; 

   }else{
       UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
       [specialLabel setHidden:YES];
   }
}

那么只有当第一个 if 语句没有通过时才会调用第二个 if 语句。这样 specialLabel.text 属性不会更改两次,并且该值不会在第二个 if 中被覆盖

关于ios - 我有两个 if 语句,第一个被跳过,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18458436/

相关文章:

iphone - 我的应用程序没有改变方向

iphone - 继续添加到 NSMutableArray

ios - SWIFT: "didBeginContact"函数中的 If 语句运行次数过多

java - 在 "if"括号内分配一个变量,如何在括号外使用该变量?

php - !$variable = $variable inside if

Java - 条件始终为真/假(Android 复选框)

php - 如何在Select查询中使用IF或Case语句?

ios - TestFlight 导入此版本时出错?

ios - 如何为 UITableView 部分创建自定义背景?

ios - 无法在 Firebase 函数中读取 null 的属性