how to insert a (BOOL *) into NSMutableDictionary
Asked Answered
T

2

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?

Trisoctahedron answered 11/11, 2011 at 0:59 Comment(1)
Why does -isSet return BOOL * instead of BOOL?Gefell
B
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];
Burberry answered 11/11, 2011 at 1:7 Comment(3)
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.Trisoctahedron
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).Burberry
Starting with Clang v3.1, we can use literals: NSNumber *yesNumber = @YES; and NSNumber *noNumber = @NO;, equivalent to [NSNumber numberWithBool:YES] etcCaldarium
P
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).

Prohibit answered 11/11, 2011 at 1:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.