Reputation: 808
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
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
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
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
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