-(void)somemethod {
BOOL x; // <--- no default value
It is initialized to garbage.
However, for a BOOLivar, 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);
}
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!