What is the default value of a BOOL variable in Objective-C?


Solution 1:

There is no default value if you write

-(void)somemethod {
  BOOL x;  // <--- no default value

It is initialized to garbage.

However, for a BOOL ivar, it will be initialized to NO, as the whole instance is filled with 0 on initialization.

(Note: When ARC is enabled, local object pointers will always be have a default value nil, but local variables of non-object types like BOOL are still initialized to garbage. See Local variables set to nil? (Objective-C).)

Solution 2:

I did some experiments of my own using Xcode 5.1, OS X Mavericks 10.9.4. For those who don’t know ALog is a modified NSLog. Anyway, first experiment was to use isLandscape as a public variable, with @synthesize, to be accessed by parent view controller (displayed below). Second experiment did not use @synthesize and I, obviously, used self.isLandscape to get the same result in the console. The console output is below my code. Third experiment used ‘isLandscape’ as a local variable inside a method.

@interface MyClass : UIView // (subclass used in my UIViewController)
…
@property (nonatomic) BOOL isLandscape;  // < - - - testing this BOOL
…

@implementation MyClass
…
@synthesize isLandscape;

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Test for YES or NO
    if (isLandscape == YES) {
        ALog(@"isLandscape == YES");
    } else if (isLandscape == NO) {
        ALog(@"isLandscape == NO");
    } else {
        ALog(@"isLandscape != YES/NO");
    }
    // Test for nil or non-nil
    if (isLandscape) {
        ALog(@"isLandscape");
    } else if (!isLandscape) {
        ALog(@"!isLandscape");
    } else {
        ALog(@"!= nil/non-nil");
    }
    // Test its value
    ALog(@"isLandscape == %d", isLandscape);
}

These results are from the first two experiments…

2014-08-28 08:18:52.909 MyApp[493:60b] -[MyClass awakeFromNib] [Line 157] isLandscape == NO
2014-08-28 08:18:52.911 MyApp[493:60b] -[MyClass awakeFromNib] [Line 166] !isLandscape
2014-08-28 08:18:52.912 MyApp[493:60b] -[MyClass awakeFromNib] [Line 172] isLandscape == 0

In the THIRD EXPERIMENT ‘isLandscape’ was no longer a property. I set it to be a local variable with interesting results:

- (void)awakeFromNib
{
    [super awakeFromNib];
    BOOL isLandscape; // < - - - testing this BOOL
    // Test for YES or NO
    if (isLandscape == YES) {
        ALog(@"isLandscape == YES");
    } else if (isLandscape == NO) {
        ALog(@"isLandscape == NO");
    } else {
        ALog(@"isLandscape != YES/NO");
    }
    // Test for nil or non-nil
    if (isLandscape) {
        ALog(@"isLandscape");
    } else if (!isLandscape) {
        ALog(@"!isLandscape");
    } else {
        ALog(@"!= nil/non-nil");
    }
    // Test its value
    ALog(@"isLandscape == %d", isLandscape);
}

These results are from the third experiment…

2014-08-28 08:28:33.483 MyApp[581:60b] -[MyClass awakeFromNib] [Line 159] isLandscape != YES/NO
2014-08-28 08:28:33.486 MyApp[581:60b] -[MyClass awakeFromNib] [Line 164] isLandscape
2014-08-28 08:28:33.487 MyApp[581:60b] -[MyClass awakeFromNib] [Line 172] isLandscape == -24

I’m guessing properties get initialized by me or Xcode automatically, but local variables get no values at all. Even so, look at [Line 164] local variable is not YES or NO but it is non-nil? I guess it is the (random) garbage value that you cannot count on. I hope this helps the next person. I learned something but I look forward to comments. Thanks and good luck!