2

I get some data from the server and save it to NSDictionary then I want to initialise NSMutableDictionary with that NSDictionary.

NSDictionary  * const received = [self serverData];
NSMutableDictionary * const results = [NSMutableDictionary dictionaryWithDictionary:received];

And If I get empty JSON from the server and consequently my NSDictionary will be empty how can I use dictionaryWithDictionary for an empty one? I use this

NSDictionary * const received = [self serverData];
NSMutableDictionary * const results;
if (![received count]) {
    results = [NSMutableDictionary dictionary];
}
else
{
    results = [NSMutableDictionary dictionaryWithDictionary:received];
}

But it seems a code smell. May be there are more elegant solutions?

  • do you get a nsdictionary object with no key-vale pair or nil? – Abid Hussain Dec 05 '12 at 15:38
  • I think that the problem is that when there is no data your dictionary is `nil` and you can't initialize a `NSMutableDictionary` from a `nil` object – tkanzakic Dec 05 '12 at 15:38
  • 1
    If you use dictionarywithDictionary: and pass in nil or an empty dictionary, you get an empty dictionary. What are you expecting it to do? – James P Dec 05 '12 at 15:39
  • 1
    if you recieve nil then you don't need to use dictionarywithdictionary. you just need to do results = [recieved mutablecopy]; if nil then it'll be ignored – Abid Hussain Dec 05 '12 at 15:39
  • @Joe Why do you need to check if received is nil, won't it return you a dictionary either way? – James P Dec 05 '12 at 15:46
  • Your right I assumed it was crashing. – Joe Dec 05 '12 at 15:48
  • 1
    That's what I'm not getting, surely `NSMutableDictionary* results = [NSMutableDictionary dictionaryWithDictionary:received];` is all that's needed. Unless i'm missing something. – James P Dec 05 '12 at 15:50
  • @AbidHussain Can you please explain me a difference between these two states "object with no key-vale pair or nil"? –  Dec 05 '12 at 15:50
  • @JamesP Yes serverdata returns NSDictionary but further I need to use NSMutableDictionary –  Dec 05 '12 at 15:51
  • It will create you an empty NSMutableDictionary with which you can do what you want. – James P Dec 05 '12 at 15:54

3 Answers3

5

You were fine in the first place.

NSDictionary* received = [self serverData];
NSMutableDictionary* results = [NSMutableDictionary dictionaryWithDictionary:received];

Will do what you want. Even if received is nil or empty, results will just be an empty NSMutableDictionary.

James P
  • 4,786
  • 2
  • 35
  • 52
1

I would make a category on NSDictionary that provides for such a safe setting.

+ (NSMutableDictionary *)safeDictionaryWithDictionary:(NSDictionary *)dictionary
{
    return [NSMutableDictionary dictionaryWithDictionary:(dictionary ? dictionary : @{});
}
phoganuci
  • 4,984
  • 9
  • 39
  • 50
0

You cant create an item in dictionary with "no key". As key must be a non-nil string.

If no items are there then no object and no key are set, this leads to setObject:nil forKey:nil

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140