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

时间:2013-08-27 06:30:44

标签: 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语句似乎完全被忽视了。

4 个答案:

答案 0 :(得分:1)

尝试这样的代码:

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

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 ([itemOnSpecial isEqualToString: @"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语句与代码中的第二个语句无关。

答案 1 :(得分:1)

如果您将代码更改为:

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属性不会被更改两次,如果

,则不会覆盖第二个值。

答案 2 :(得分:0)

第一个语句将检查区分大小写的字符串,而第二个语句将检查不区分大小写的字符串。

当您的itemOnSpecial's值仅等于@"是"在这种情况下,它将进入内部,否则它将跳过,而在第二种情况下,你的字符串等于@"是",@"是",@" yEs&# 34;,@"是"在任何情况下它都会进入。

所以我希望你明白我的回答......

答案 3 :(得分:0)

在第一个if条件上设置断点并打印条件以查看它返回的内容。然后完成单步调试。

离。

(lldb) p ([itemOnSpecial caseInsensitiveCompare: @"yes"] == 0)
(bool) $1 = true

NSOrderedSame == 0。