How to compare @NO and @YES with elegance and without risk of false positives/negatives?
boolean, comparison, ios, objective-c
Solution
What is `condition`? Is it a `BOOL` or an `NSNumber`?
If `condition` is a `BOOL`, then you don't want to use `@NO` or `@YES` at all. You want to say
if (condition) // test if condition is true
if (!condition) // test if condition is false
if (condition == NO) // same as previous, based on personal preference
Note that you should never say
if (condition == YES)
because `BOOL` isn't actually restricted to `0` and `1` as values, it can hold anything in `char`, so if `condition` accidentally holds, say, `3`, then `if (condition)` and `if (condition == YES)` would behave differently.
If `condition` is an `NSNumber`, then you still don't want to use `@NO` and `@YES`. You just want to convert it to a `BOOL` using `-boolValue`, as in
if ([condition boolValue]) // test if condition is true
if (![condition boolValue]) // test if condition is false
if ([condition boolValue] == NO) // same as previous, based on personal preference
The basic takeaway here is, don't use `@NO` and `@YES` literals for comparisons. It's pointless, and inelegant, since all you'd be able to do with them is convert them back into `BOOL`s.
Problem
I want to use bool literals like ``` if (condition == @NO) { } else if (condition == @YES) { { ``` When I try this, XCode wants me to use NSNumber methods to compare, like isEqualTo. Is there a simpler way to do this (without isEqualTo)? If I can't, should I use isEqualTo, isEqualToValue, or isEqualToNumber?