0

I have a program in which I want to flip a BOOL value without doing this:

@property (nonatomic, assign) BOOL *boolValue;

if (boolValue == YES) {
    boolValue = NO;
}
if (boolValue == NO) {
    boolValue = YES;
}

Is there a method I can use to switch the value without using this way?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
av993
  • 73
  • 1
  • 12
  • [related](http://stackoverflow.com/questions/541289/objective-c-bool-vs-bool); looks like you can just `boolValue ^= 1`. – Kenney Oct 27 '15 at 00:24
  • Kenney has the right idea. At the very least, for the second statement you can use an `else`. – intcreator Oct 27 '15 at 00:44

1 Answers1

4

You have several things wrong here.

  1. Don't make the property a BOOL *, make it just a BOOL.
  2. Never check a BOOL value directly against YES or NO.
  3. Toggling a BOOL is done by boolValue = !boolValue;.

Your code becomes:

@property (nonatomic, assign) BOOL boolValue;

boolValue = !boolValue; // "flip" the BOOL value

BTW - for point #2 you should write such if statements like this:

if (boolValue) { // is it YES?

or:

if (!boolValue) { // is it NO?

But neither are needed to flip the value.

rmaddy
  • 314,917
  • 42
  • 532
  • 579