13
votes

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?

2
Why does -isSet return BOOL * instead of BOOL? - user557219

2 Answers

41
votes

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];
0
votes

Your isSet method would need to have the following signature: - (BOOL)isSet;

Assuming that is the case, there shouldn't be any problem using NSNumber as mentioned by titaniumdecoy.

Your last sentence intrigues me, BOOL *. Surely you mean BOOL, if you absolutely need a boolean reference, then I would suggest you store the initial/actual BOOL in a NSNumber and store the references that object wherever you'd need it (i.e. your NSMutableDictionary).