AlexR
AlexR

Reputation: 5634

Comparing two BOOL values

In my instance method, would like to compare a BOOL parameter with the content of a static variable, for instance:

- (NSArray*)myMethod:(NSString*)someString actualValuesOnly:(BOOL)actualValuesOnly {
static NSString *prevSsomeString;
static BOOL prevActualValuesOnly;
static NSArray *prevResults

if ([someString isEqualToString:prevSomeString] && 
              ([actualValuesOnly isEqual: prevActualValuesOnly]) 
               // HOW TO COMPARE THESE TWO BOOLEANS CORRECTLY?? 
    { return prevResults; }// parameters have not changed, return previous results 
else { } // do calculations and store parameters and results for future comparisons)

What would be the correct way to do this?

Upvotes: 0

Views: 7674

Answers (5)

James Kuang
James Kuang

Reputation: 10730

The solutions mentioned here are not the safest way to compare 2 BOOL values, because a BOOL is really just an integer, so they can contain more than just YES/NO values. The best way is to XOR them together, like detailed here: https://stackoverflow.com/a/11135879/1026573

Upvotes: 1

Evol Gate
Evol Gate

Reputation: 2257

As Matthias Bauch suggests,

Simply do the comparison using == operator i.e

if (BOOL1 == BOOL2)   
{   
    //enter code here
}

Upvotes: 0

ca android
ca android

Reputation: 51

Boolean variable is compare with == sign instead of isEqual

if(Bool1 == Bool2){

    // do something here}

Upvotes: 3

Arvind Kanjariya
Arvind Kanjariya

Reputation: 2097

Boolean is compare with == sign instead of isequal:

Upvotes: 1

Matthias Bauch
Matthias Bauch

Reputation: 90117

Since BOOL is a primitive (or scalar) type, and not a class, you can compare it directly with ==

if ([someString isEqualToString:prevSomeString] && actualValuesOnly == prevActualValuesOnly) 

Upvotes: 5

Related Questions