Let’s say I have a dictionary full of objects for keys that may or may not be there. What is the standard practice for checking whether this key exists or not?
For example, what I’ve written looks like this:
id temp;
temp = [dict objectForKey: @"id"];
if (temp != [NSNull null]) {
uid = [temp intValue];
}
temp = [dict objectForKey: @"name"];
if (temp) {
[name release];
name = [[NSString alloc] initWithString: temp];
}
temp = [dict objectForKey: @"latitude"];
if (temp != [NSNull null]) {
[latitude release];
latitude = [[NSNumber alloc] initWithDouble: [temp doubleValue]];
}
temp = [dict objectForKey: @"longitude"];
if (temp != [NSNull null]) {
[longitude release];
longitude = [[NSNumber alloc] initWithDouble: [temp doubleValue]];
}
Should I surround this code with an autorelease pool? Do I have to release each time I point temp to an object in the dictionary, or does the pool handle that automatically? Is there a better way to handle errors?
I use the method described in this post: How to map JSON objects to Objective C classes?