Reputation: 737
The value of BOOL in objective C is always NO(by default). But recently I encountered a case where the value of BOOL variable was returning YES (by default). Can anybody explain this to me ?
Upvotes: 4
Views: 6587
Reputation: 1578
If you declare a variable without initialization, the OS will allocate memory randomly and at that instant allocated memory may contain some garbage value. That garbage can represent No also or may be Yes value.
Recommended: Always declare a variable with proper initialization to avoid bugs in your code.
Upvotes: 1
Reputation: 81848
BOOL
has no value as it is a type.
You probably mean variables of type BOOL. There are different types of variables, which have different initialization semantics:
alloc
promises to set all instance variables to zero, which in case of BOOL means NO
;Upvotes: 23