Reputation: 4540
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
Upvotes: 19
Views: 20871
Reputation: 122391
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
}
Upvotes: 57