views:

23

answers:

2

I use Objective-C bindings for YAJL in my Mac OS X application.

I could not find out how to insert a boolean value (to appear as key:true in the JSON string) in my NSDictionary:

NSMutableDictionary* jsonDict = [NSMutableDictionary dictionary];
[jsonDict setValue: YES forKey: @"key"];

The code above does not run (obviously because YES is not an object).
How can I accomplish this?

+2  A: 

+[NSNumber numberWithBool:] is the typical way to add a boolean to a NSDictionary.

Shawn Craver
+2  A: 

You insert booleans into a dictionary using NSNumber:

NSNumber *yes = [NSNumber numberWithBool:YES];
NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:yes forKey:@"key"];

You can also use (id)kCFBooleanTrue, which is arguably shorter but uglier and less obvious.

Jeremy W. Sherman