max_
max_

Reputation: 24481

Reversing a BOOL

In objective-c, if I wanted to reverse the value of a BOOL, would this work, or would it set the value of the BOOL to NO?

BOOL ab = YES;

ab = !ab; // would this reverse the BOOL, ab, and set it to NO?

if (ab == NO) {
    ab = !ab; // would this reverse the BOOL again and set it to YES?
}

Upvotes: 4

Views: 5450

Answers (4)

okocsis
okocsis

Reputation: 1

These are correct!

  1. !YES == NO
  2. !NO == YES
  3. !1 == NO
  4. !0 == YES
  5. !5 == NO

This is not correct!

FreeAsInBeer: "YES is equal to anything else (except NULL)." - NOT TRUE

YES equals to nothing but the signed char 1, so YES == 32626 for instance returns false!!

Upvotes: 0

FreeAsInBeer
FreeAsInBeer

Reputation: 12979

BOOLs work exactly as you have described. Not that NO is 0 and YES is equal to anything else (except NULL).

Examples:

  1. !YES == NO
  2. !NO == YES
  3. !1 == NO
  4. !0 == YES
  5. !5 == NO

Upvotes: 1

user756245
user756245

Reputation:

quick question, quick answer. yes

Upvotes: 2

rid
rid

Reputation: 63472

Yes, that will work. BOOL is just an integer. YES is 1 and NO is 0. !1 == 0 and !0 == 1.

Upvotes: 8

Related Questions