Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOOL wrapper? Make an object of `BOOL` value. (Objective-C)

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?

like image 304
Shade Avatar asked May 12 '11 09:05

Shade


People also ask

How do I set boolean value in Objective C?

To set a BOOL, you need to wrap the number in a value object with [NSNumber numberWithBool:NO] .

Can BOOL be nil Objective C?

You can't. A BOOL is either YES or NO . There is no other state.

What is the default value of BOOL in Objective C?

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.

What is Boolean in Objective C?

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.


1 Answers

NSNumber *boolForUserInfo = @YES; // or [NSNumber numberWithBool:YES] the old way [userInfo setObject:boolForUserInfo forKey:@"myBool"]; 

Retrieve with:

[[userInfo objectForKey:@"myBool"] boolValue]; 
like image 166
Eimantas Avatar answered Sep 30 '22 09:09

Eimantas