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
}
Share:
19,805

Related videos on Youtube

smartsanja
Author by

smartsanja

Updated on June 29, 2022

Comments

  • smartsanja
    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
      Adil Soomro over 10 years
      You could save [NSNumber numberWithBool:YES]; and later [[dictionary objectForKey:@"key"] boolValue];
  • Vidhi
    Vidhi almost 9 years
    I have one Bool variable Like Bool fbId; dicTemp[@"keyValue"] = fbId; This is Not Work please help me .. Thanks in Advance
  • trojanfoe
    trojanfoe almost 9 years
    @Vidhi Start a new question.

Related