Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store “bool” values in an NSMutableArray* or NSMutableDictionary*? Chronic problems with JSON data that come in as bool's

This question is similar to: How do you store "int" values in an NSMutableArray* or NSMutableDictionary*? Chronic problems with JSON data that come in as integers.

How do you do this with Boolean values?

How do you store "bool" values in an NSMutableArray* or NSMutableDictionary*? Chronic problems with JSON data that come in as Boolean values.

like image 831
MikeN Avatar asked Jan 23 '11 21:01

MikeN


3 Answers

You can store it as a number:

[mutableArray addObject[NSNumber numberWithBool:YES]];

EDIT And with the new syntax:

 //BOOL
[mutableArray addObject:@YES];
 //integer
 [mutableArray addObject:@23]; 
like image 91
shannoga Avatar answered Nov 19 '22 06:11

shannoga


You should simply wrap the bool in an NSNumber as follows:

NSNumber *wrappedBool = [NSNumber numberWithBool:YES];

For future reference, NSNumber has quite a few numberWith... methods for wrapping booleans, ints, floats, etc.

like image 40
John Parker Avatar answered Nov 19 '22 05:11

John Parker


You can also use JSON-Framework which abstracts away all those difficulties.

like image 1
kubi Avatar answered Nov 19 '22 07:11

kubi