13

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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Suchi
  • 9,989
  • 23
  • 68
  • 112

2 Answers2

41

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];
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
  • As you can see in my question I am already doing that but it's not working for me. That is when I get the error that I have mentioned. – Suchi Nov 11 '11 at 01:09
  • Are you sure `[aMessage isSet]` is returning a BOOL and not a BOOL*? If it returns the latter, you need to use `[NSNumber numberWithBool:*[aMessage isSet]]` (although you should first check that it is not NULL). – titaniumdecoy Nov 11 '11 at 01:17
  • 2
    Starting with Clang v3.1, we can use literals: `NSNumber *yesNumber = @YES;` and `NSNumber *noNumber = @NO;`, equivalent to `[NSNumber numberWithBool:YES]` etc – coco May 10 '13 at 23:00
0

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).