What is the default value of a BOOL
variable in Objective-C?
The default value of boolean data type in Java is false, whereas in C++, it has no default value and contains garbage value (only in case of global variables, it will have default value as false).
C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.
There are just two values of type bool: true and false. They are used as the values of expressions that have yes-or-no answers. C++ is different from Java in that type bool is actually equivalent to type int. Constant true is 1 and constant false is 0.
True and False are specific bool values. Use default False when you have a bool field and you want the default to be False.
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).)
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With