Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set BOOL value to NSMutableDictionary

I want to set a BOOL value as true false format to a NSMutableDictionary (which will use as a JSON Dictionary of the API Request). I tried both of the following methods. But those are NOT the correct format requested by the API (API needs the BOOL value as true/false type not as 1/0).

BOOL isDefault = YES;
[dicTemp setValue:[NSString stringWithFormat:@"%c", isDefault] forKey:@"is_default"];

//[dicTemp setValue:[NSNumber numberWithBool:isDefault] forKey:@"is_default"];

Can anyone have an ides

like image 849
smartsanja Avatar asked Sep 24 '13 05:09

smartsanja


1 Answers

BOOL can be wrapped in an NSNumber object:

dicTemp[@"is_default"] = @YES;

(This code is using the newish Objective-C Literals syntax).

If you are old-fashioned (nothing wrong with that), then the above statement is the same as:

[dicTemp setObject:[NSNumber numberWithBool:YES]
            forKey:@"is_default"];

To test later:

NSNumber *numObj = dicTemp[@"is_default"];
if ([numObj boolValue]) {
    // it was YES
}
like image 73
trojanfoe Avatar answered Oct 12 '22 01:10

trojanfoe