how to insert a (BOOL *) into NSMutableDictionary

Suchi picture Suchi · Nov 11, 2011 · Viewed 14.3k times · Source

I am trying to save a boolean database value into a map as follows -

[recentTags setValue:[NSNumber numberWithBool:[aMessage isSet]] forKey:[aMessage tagName]];

It gives me an error saying "Incompatible pointer to integer conversion sending BOOL * aka signed char* to 'BOOL' aka signed char"

How would I insert a BOOL* into the dictionary?

Answer

titaniumdecoy picture titaniumdecoy · Nov 11, 2011

Wrap the BOOL in an NSNumber:

NSNumber *boolNumber = [NSNumber numberWithBool:YES];

To get it out:

BOOL b = [boolNumber boolValue];

You can wrap other non-object types (such as a pointer or a struct) in an NSValue.


EDIT: Assuming you really mean a BOOL* (pointer):

NSValue *boolValue = [NSValue value:pointerToBool withObjCType:@encode(BOOL*)];
BOOL *b = [boolValue pointerValue];