开发者

curious what is wrong with this if statement?

Could someone explain what is wrong with this "if statement"? I get:

"lvalue required as left operand of assignment".

This does not work:

if ([[prepDataArray objectAtIndex:iNDEX] boolValue] = YES) {
        NSLog(@"HARD");
    }
开发者_如何学Python

While this works:

diffQ = [[prepDataArray objectAtIndex:iNDEX] boolValue];
    if (diffQ = YES) {
        NSLog(@"HARD");
    }

I do realize where the problem are and that the 'lvalue' indicate that i need to have something different on the left side but i do not grasp why and how to do what i want inside the 'if' statement as tried in the first example.

Would appreciate if someone nice could give me a hint :-)


if ([[prepDataArray objectAtIndex:iNDEX] boolValue] == YES) {
    NSLog(@"HARD");
}

it's == not =

The first one doesn't work because you try to assign a BOOL (YES) to a message. The second one works because you try to assign a BOOL to diffQ. This is correct, but not the result you expect (comparing diffQ to YES)

Common programming error ;) I've done this a millions times


I completely agree with what @thomas said above, but let me add.

Don't compare a bool to YES. It's not that the if construct requires

if( some comparison statement ) {
      ....
}

That's not the case. The if construct has the following form:

if( a boolean value) {
      ...
}

It just happens that a comparison statement yields a boolean, so you put that in the if statement. In your case, boolValue already gives you a bool. You don't need to compare that against YES or NO. That's like doing YES==YES or YES==NO and it's completely redundant.

Just do

if ([[prepDataArray objectAtIndex:iNDEX] boolValue]) {
    NSLog(@"HARD");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜