Shayno
Shayno

Reputation: 808

I have 2 if statements in one, the first is being skipped, why?

Hi there I'm having trouble using multiple if statements. Here is my code:

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];
}

Only the second if statement is taken into account. The first if statement seems to be completely disregarded.

Upvotes: 2

Views: 161

Answers (4)

No Idea For Name
No Idea For Name

Reputation: 11577

if you'll change your code to:

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];
   }
}

then the second if statement will be called only if the first didn't pass. that way the specialLabel.text property will not be changed twice and the value will not be overridden in the second if

Upvotes: 1

gaijin
gaijin

Reputation: 185

Set a breakpoint on the first if condition and print the condition to see what it's returning. Then finish stepping through the code.

ex.

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

Where NSOrderedSame == 0.

Upvotes: 0

Rawrrr1337
Rawrrr1337

Reputation: 261

Try the code like this:

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];
}
}

Or like this:

   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];
    }

I don't understand your script, but the first if statement have nothing to do with the second one in your code.

Upvotes: 1

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

The first statement will check case sensitive string while second statement will check case insensitive string.

when your itemOnSpecial's value is equal to only @"yes" in that case it will be enter inside otherwise it will skip while in the second case your string is equal to like @"Yes",@"YES",@"yEs",@"yeS" in all cases it will enter.

So i hope u understand my answer...

Upvotes: 0

Related Questions