Jazzmine Jazzmine - 8 months ago 69
Objective-C Question

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.


Answer Source

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;