Skip to content

Instantly share code, notes, and snippets.

@kmdarshan
Created March 9, 2015 04:42
Show Gist options
  • Select an option

  • Save kmdarshan/fec1374b5c160abf51be to your computer and use it in GitHub Desktop.

Select an option

Save kmdarshan/fec1374b5c160abf51be to your computer and use it in GitHub Desktop.
Checking for null objects
@implementation RRUser
@synthesize email, name, facebookId, userId, sessionToken, imageUrl;
-(void) parseResponse:(NSDictionary*) data {
// this is a better way to test, if the class responds to a selector
// then go ahead and read from it
if([[data objectForKey:@"email"] respondsToSelector:@selector(length)]) {
self.email = [data objectForKey:@"email"];
}
if([[data objectForKey:@"name"] respondsToSelector:@selector(length)]) {
self.name = [data objectForKey:@"name"];
}
// incase you read from self.name, it would crash, since name would be null
if([data objectForKey:@"name"] != Nil) {
self.name = [data objectForKey:@"name"];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment