How do I wrap a BOOL
in an object type in Objective-C?
I want to store a BOOL
in the userInfo
object of an NSTimer
. How do I wrap it?
To set a BOOL, you need to wrap the number in a value object with [NSNumber numberWithBool:NO] .
You can't. A BOOL is either YES or NO . There is no other state.
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.
Boolean, in Objective-C, is a hold over data type from the C language. Both in C, and hence in Objective-C, 0 (zero) is treated as “false” and 1 (one) as “true”. C has a Boolean data type, bool (note: the lowercase), which can take on the values of true and false.
NSNumber *boolForUserInfo = @YES; // or [NSNumber numberWithBool:YES] the old way [userInfo setObject:boolForUserInfo forKey:@"myBool"];
Retrieve with:
[[userInfo objectForKey:@"myBool"] boolValue];
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