jump to navigation

NSDictionary objectAtKey returns (null)? July 31, 2009

Posted by Ameya in iPhone.
Tags: , , , ,
trackback

A small work around for problem faced with NSDictionary null return problem.

In cases like

NSString *myImageURL = [user objectForKey:@"imageURL"];

where [user objectForKey:@"imageURL"] may return NSNull object.

This makes myImageURL a NSNull object hence cannot be compared using conventional comparisons. An NSNull is an invalid memory allocation or an error.:)

Two alternative solutions are

if ((NSNull *)[user objectForKey:@"imageURL"] == [NSNull null]) { }

and to convert to string if the above does not works.

NSString *myImageURL = [NSString stringWithFormat:@"%@",[user objectForKey:@"imageURL"]];

this can be compared by

if([imgURL isEqualToString:@""]){
}

Advertisement

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.