Set BOOL value to NSMutableDictionary
19,805
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
}
Related videos on Youtube
Author by
smartsanja
Updated on June 29, 2022Comments
-
smartsanja almost 2 years
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
-
Adil Soomro over 10 yearsYou could save
[NSNumber numberWithBool:YES];
and later[[dictionary objectForKey:@"key"] boolValue];
-
-
Vidhi almost 9 yearsI have one Bool variable Like Bool fbId; dicTemp[@"keyValue"] = fbId; This is Not Work please help me .. Thanks in Advance
-
trojanfoe almost 9 years@Vidhi Start a new question.