Part of my app gets JSON
data and uses it to display updates. There's two parts to the initial JSON
response: an error and an update section.
The following is the array you get when doing [JSONData objectForKey:@"updates"];
to isolate the update section
{
2 = {
message = "Best restaurant ever!";
name = " ";
profilePictureURL = "<null>";
restPictureURL = "http://i.imgur.com/QpyMRwY.jpg";
restaurant = "someplace";
timestamp = 1390194000;
};
3 = {
message = "Ehh.";
name = " ";
profilePictureURL = "<null>";
restPictureURL = "http://i.imgur.com/QpyMRwY.jpg";
restaurant = "someplace";
timestamp = 1389848400;
};
4 = {
message = "";
name = " ";
profilePictureURL = "<null>";
restPictureURL = "http://i.imgur.com/QpyMRwY.jpg";
restaurant = "McDonald's";
timestamp = 1390346335;
};
5 = {
message = "Service was slow.";
name = " ";
profilePictureURL = "<null>";
restPictureURL = "http://i.imgur.com/QpyMRwY.jpg";
restaurant = "Bad pizzaplace";
timestamp = 1389330000;
};
}
Then I pass this NSArray
to a function which formats it to a class I designed for updates and then appends that update to a NSMutableArray
stored in AppDelegate
.
@implementation Update
+(void) appendUpdates:(NSArray*)data
{
AppDelegate* appDelegate= [[UIApplication sharedApplication]delegate];
for (NSDictionary* currentUpdate in data)
{
Update* formattedUpdate = [[Update alloc]initWithDict:currentUpdate];
[appDelegate.updateList addObject:formattedUpdate];
}
}
-(id) initWithDict:(NSDictionary*)update
{
self = [super init];
self.name = [update objectForKey:@"name"];
self.restaurant = [update objectForKey:@"restaurant"];
self.message = [update objectForKey:@"message"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(int)[update objectForKey:@"timestamp"]];
self.timestamp = date;
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date];
self.dateComponents = components;
self.profilePictureURL = [update objectForKey:@"profilePictureURL"];
self.restaurantPictureURL = [update objectForKey:@"restPictureURL"];
return self;
}
@end
When I run the code, I receive an error inside the initialization method when trying to use objectForKey:
.
Any ideas? Thanks in advance