Jazzmine
Jazzmine

Reputation: 1875

How to set NSMutableDictionary to null

I am using a NSMutableDictionary and am checking to see if it is empty (specifically null) using this:

if( [webContent isKindOfClass:[NSNull class]])

If it is true, I make a call to a web service and populate it with values that I process to display to the user.

Then I want to reset it to null for the next loop but I'm not sure how to reset it. I assume that reinitializing it like this:

NSMutableDictionary * webContent = [[NSMutableDictionary alloc]init];

has more overhead than setting it to null but please offer your advice.

Thanks

Upvotes: 0

Views: 767

Answers (2)

Sunil Chauhan
Sunil Chauhan

Reputation: 2084

NSMutableDictionary * webContent = nil; You can set null to values of the dictionary, not the collection itself:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNull null] forKey:@"myKey"];

Upvotes: 1

Borys Verebskyi
Borys Verebskyi

Reputation: 4268

From Apple documentation:

The NSNull class defines a singleton object used to represent null values in collection objects (which don’t allow nil values).

For you, it's not the case. You should use nil instead of NSNull. Then testing will simply look like

if (webContent) {
    // do something
}

and reset like

webContent = nil;

Upvotes: 2

Related Questions