73

OK, I'm a little confused. It's probably just a triviality.

I've got a function which looks something like this:

- (void)getNumbersForNews:(BOOL)news andMails:(BOOL)mails {
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:news  forKey:@"getNews"];
[parameters setValue:mails forKey:@"getMails"];...}

It doesn't matter whether I use setValue:forKey: or setObject:ForKey:, I'm always getting a warning:

"Passing argument 1 of set... makes pointer from integer without a cast"...

How on earth do I insert a bool into a dictionary?

6 Answers 6

136

Values in an NSDictionary must be objects. To solve this problem, wrap the booleans in NSNumber objects:

[parameters setValue:[NSNumber numberWithBool:news] forKey:@"news"];
[parameters setValue:[NSNumber numberWithBool:mails] forKey:@"mails"];
4
  • 10
    And don't forget that tarting with Clang v3.1, we can use literals: NSNumber *yesNumber = @YES; equivalent to [NSNumber numberWithBool:YES]
    – coco
    Commented May 10, 2013 at 23:03
  • 9
    So, as @coco says, you can use @YESand @NO as substitutes for [NSNumber numberWithBool:YES] and [NSNumber numberWithBool:NO] respectively. And as @AlBlue pointed out below, the way to retrieve the value from the dictionary later would be to use [[myDictionary objectForKey:theKey] boolValue].
    – RoberRM
    Commented Sep 6, 2013 at 0:25
  • 1
    No No No. You are using setValue, which is not an NSMutableDictionary method, but a KVO method. Use setObject instead. Commented May 23, 2016 at 19:11
  • 1
    Just a note regarding the setObject:forKey method - Objective C will actually let you call setObject:false. The reason is that false is replaced with 0, which is equivalent to nil. So by setting an object to false, you are setting it to nil, and setting an object in a dictionary to nil actually removes the entry from the dictionary. So its fairly dangerous in other words and results in highly unexpected behaviour. Commented Jun 5, 2018 at 5:27
14

Objective-C containers can store only Objective-C objects so you need to wrap you BOOL in some object. You can create a NSNumber object with [NSNumber numberWithBool] and store the result.
Later you can get your boolean value back using NSNumber's -boolValue.

13

Modern code for reference:

parameters[@"getNews"] = @(news);
0
8

You can insert @"YES" or @"NO" string objects and Cocoa will cast it to bool once you read them back.

Otherwise I'd suggest creating dictionary using factory method like dictionaryWithObjectsAndKeys:.

2
  • That sounds dangerous -- what if you wanted the string? Commented Nov 1, 2011 at 19:57
  • I might be mistaken, but using boolValue and stringValue would yield needed results. Need a boolean? Use boolValue. Need a string? Go with stringValue.
    – Eimantas
    Commented Nov 2, 2011 at 6:35
8

A BOOL is not an object - it's a synonym for an int and has 0 or 1 as its values. As a result, it's not going to be put in an object-containing structure.

You can use NSNumber to create an object wrapper for any of the integer types; there's a constructor [NSNumber numberWithBool:] that you can invoke to get an object, and then use that. Similarly, you can use that to get the object back again: [obj boolValue].

3

Seeing @Steve Harrison's answer I do have one comment. For some reason this doesn't work with passing object properties like for e.g.

 [parameters setValue:[NSNumber numberWithBool:myObject.hasNews] forKey:@"news"];

This sets the news key to null in the parameter NSDictionary (for some reason can't really understand why)

My only solution was to use @Eimantas's way as follows:

[parameters setValue:[NSNumber numberWithBool:myObject.hasNews ? @"YES" : @"NO"] forKey:@"news"];

This worked flawlessly. Don't ask me why passing the BOOL directly doesn't work but at least I found a solution. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.