在我的IF语句中,它有点奇怪,因为每次我的代码在其中循环时,只调用SLOT1并在我的调试器中显示,为什么呢?我的陈述有问题吗?感谢。
choiceSlot1 = TRUE;
choiceSlot2 = TRUE;
choiceSlot3 = TRUE;
choiceSlot4 = TRUE;
if (slot1 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot1 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot1 ) {
NSLog(@"Slot1");
choiceSlot1 = FALSE;
}
else if (slot2 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot2 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot2) {
NSLog(@"Slot2");
choiceSlot2 = FALSE;
}
else if (slot3 != [carousel1 indexOfItemViewOrSubview:carousel1.superview] || twoSlot3 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot3) {
NSLog(@"Slot3");
choiceSlot3 = FALSE;
}
else if (slot4 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot4 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot4) {
NSLog(@"Slot4");
choiceSlot4 = FALSE;
}
答案 0 :(得分:1)
因为else if
仅在第一个if
语句为false
时才会得到验证。
示例:
if (true)
{
// Will be executed
}
else if (true)
{
// Will not be executed
}
if (false)
{
// Will not be executed
}
else if (true)
{
// Will be executed
}
答案 1 :(得分:1)
很容易,
if (slot1 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot1 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot1 )
如果choiceSlot1为TRUE,则第一个语句总是为真(带有OR,如果其中一个元素为真,则为true)。
当第一个语句为真时,则不调用else / if的其余部分!