0

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?

AlexR
  • 5,514
  • 9
  • 75
  • 130

5 Answers5

5

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

if ([someString isEqualToString:prevSomeString] && actualValuesOnly == prevActualValuesOnly) 
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • Hi Matthias, thanks for your answer. I thought `BOOL`s are stored as `NSNumber` objects. Wouldn't `==` just compare their pointers and not their actual values? – AlexR Oct 26 '12 at 08:00
  • BOOL is a primitive type like for example int or float. Compare it directly. http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/FoundationTypesandCollections/FoundationTypesandCollections.html – Matthias Bauch Oct 26 '12 at 08:02
3

Boolean variable is compare with == sign instead of isEqual

if(Bool1 == Bool2){

    // do something here}
ca android
  • 51
  • 2
1

Boolean is compare with == sign instead of isequal:

Arvind Kanjariya
  • 2,089
  • 1
  • 18
  • 23
1

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

Community
  • 1
  • 1
James Kuang
  • 10,710
  • 4
  • 28
  • 38
0

As Matthias Bauch suggests,

Simply do the comparison using == operator i.e

if (BOOL1 == BOOL2)   
{   
    //enter code here
}
Evol Gate
  • 2,247
  • 3
  • 19
  • 37