I'm trying to pull the JSON data from fields "field_message_subject" and "field_message_body" to a table cell in my app. I'm using the following code to do it, and the data is returned successfully, but my app crashes and the label isn't populated. The crash error is:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *entityData = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"1"] forKey:@"uid"];
[DIOSEntity
entityGet:entityData
name:@"entity_message"
eid:@"uid"
success:^(AFHTTPRequestOperation *op, id response) {
self.messages = [NSMutableArray arrayWithObject:(NSDictionary*)response];
dispatch_async(dispatch_get_main_queue(), ^(void){
[self.tableView reloadData];
});
}
failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"failed to get data"); }
];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *PointsTableIdentifier = @"MyMessagesCell";
MyMessagesCell *cell = (MyMessagesCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyMessagesCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row];
[[cell subjectLine] setText:[receivedSubjectLine objectForKey:@"field_message_subject"]];
NSLog(@"Received message subject is here %@", receivedSubjectLine);
return cell;
}
See the returned JSON format below:
2016-02-02 10:37:19.996 app[4208:1406692] Messages are as follows (
{
arguments = (
);
data = (
);
"field_message_body" = {
und = (
{
format = "<null>";
"safe_value" = "Testing message center";
value = "Testing message center";
}
);
};
"field_message_group_ref" = (
);
"field_message_subject" = {
und = (
{
format = "<null>";
"safe_value" = Testing;
value = Testing;
}
);
};
"field_message_user_ref" = (
);
language = en;
mid = 1;
"rdf_mapping" = (
);
timestamp = 1447260780;
type = "private_message";
uid = 1;
}
)