Objective-C and the Properties of BOOL

At a basic level, boolean logic allows for a value to be either true or false, on or off, 1 or 0. The ways in which boolean types are implemented vary widely across languages and implementations. At the most granular level, computers work with bits that are either set to on or off, whereas some boolean data types in databases cater for three or even four values: 1, 0, null or undefined. Dennis Ritchie’s original C language lacked a boolean type at all but it was added in C99, and C’s primitive ‘bool’ type is available in Objective-C. ‘bool’ can be set to either true or false, 1 or 0.

Objective-C has implemented a more standard boolean property – it offers the BOOL type in addition to the primitive ‘bool’. The Cocoa and Cocoa Touch API’s use BOOL as the standard message response in favour of ‘bool’, so it is a property you must get to understand, and should code your own methods using BOOL as well.

Unlike most other things in Objective-C, BOOL is NOT an object, it is a raw data type. The standard values of BOOL are YES and NO rather than true or false, or 1 or 0. However, under the bonnet YES and NO are simply definitions given to the preprocessor as aliases for 1 and 0. In Cocoa Touch YES and NO are defined in NSObjCRuntime.h:-

#if !defined(YES)
    #define YES (BOOL)1
#endif

#if !defined(NO)
    #define NO (BOOL)0
#endif

This means that BOOL values are NEVER ‘YES’ and ‘NO’ so you cannot test them as such:-

if (myBOOL == 'YES') { ... }

…and they are NOT objects, so you cannot send the message:-

if ([myBOOL isEqualToString: @"YES"]) { ... }

Instead you must test them for true or false, or zero or non-zero:-

if (myBOOL) { ... }

or

if (!myBOOL) { ... }

If you want to log the value of a BOOL property using NSLog, then you can use the decimal signed integer format identifier %d:-

NSLog(@"MyBOOL value is %d", myBOOL);

Because BOOL represents 1 and 0 internally, you can also use the ternary operator to test BOOL values:-

myBOOL ? NSLog(@"Yes") : NSLog(@"No");

…and you can use this technique to convert between bool and BOOL should you so wish:-

mybool = myBOOL ? true : false;

Remembering that BOOL is NOT an object type makes it easy to remember how to declare BOOL properties, namely:-

BOOL myBOOL = YES;

…rather than using a pointer:-

BOOL *myBOOL = YES;

BOOL properties can be set with any of YES / NO, true / false or 1 / 0. All map back to 1 or 0.

Understanding that Objective-C’s BOOL data type is simply mapped to 1 and 0 makes working with BOOL values completely straightforward!

Share this:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • MySpace
  • StumbleUpon
  • Twitter
  • Yahoo! Buzz

This post was written by:

- who has written 11 posts on Mindsizzlers.


Contact the author

2 Responses to “Objective-C and the Properties of BOOL”

  1. Zsolt says:

    Thanks for the quick tutorial Stuart.
    Would you consider the code below as correctly using BOOL?
    Would you go through property to set the BOOL var? I need to change the variable both from within the class and from a different class.

    BOOL gatheringFeaturedDataBool;
    @property (readwrite, assign) BOOL gatheringFeaturedDataBool;

    @synthesize gatheringFeaturedDataBool;

    self.gatheringFeaturedDataBool = NO;

    if (!self.gatheringFeaturedDataBool) {
    [self networkCheckAndDecide];
    }

    Thanks!

  2. Stuart says:

    ‘Self’ is used to access properties for objects through accessors; without the use of ‘self’ you end up setting an instance variable rather than a property.

    Because BOOL is essentially a primitive type, ‘self’ isn’t necessary – a BOOL is an instance variable rather
    than a property. As such, a property declaration is only necessary if access is needed from other objects. Internally, ‘self.myBOOL’ and ‘myBOOL’ reduce to the same thing because the accessor for primitive types should simply be ‘assign’.

    Both work as you would expect and because we are dealing with primitives, there’s no difference in the outcome between these two forms. I’m not aware of any convention here amongst Objective-C programmers, so my guess is that it comes down to your own preference!

Trackbacks/Pingbacks


Leave a Reply

Advert

For more information about our services…

Contact Us

Wordle of the Day

Image from software at http://wordle.net
Data by Web Trends Now

Categories