我对Objective C中的开发非常陌生,我正在努力弄清楚为什么在将NSDictionary应用于表视图时会出现错误。我收到错误"在'NSDictionary*类型的对象上找不到读取数组元素的预期方法"。有人知道这意味着什么吗?或者我会如何解决它?我已经包括了我遇到问题的模型。
更新时间:被标记的语句是:
NSDictionary *conversation = self.data[indexPath.row];
谢谢!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ConversationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"conversationCell" forIndexPath:indexPath];
// Get data from array at position of the row
NSDictionary *conversation = self.data[indexPath.row];
// Apply the data to each row
cell.conversationRecipients.text = [conversation valueForKey:@"recipients"];
cell.conversationPreview.text = [conversation valueForKey:@"last_message"];
cell.conversationTime.text = [conversation valueForKey:@"last_thread_post_short"];
//Set conversation thumbnail
[cell.conversationThumbnail sd_setImageWithURL:[conversation valueForKey:@"sender_avatar"]];
return cell;
}
这是我设置自我的地方。数据:
// Successful Login
// Create the URL from a string.
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"URLISHERE"]];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
[request setHTTPMethod:@"GET"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// Prepare for the response back from the server
NSHTTPURLResponse *response = nil;
NSError *error = nil;
// Send a synchronous request to the server (i.e. sit and wait for the response)
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSError *serializeError;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&serializeError];
// Get response
self.data = json;
NSLog(@"%@", self.data);
// Reload data after Get
[self.tableView reloadData];
self.data
的类型必须是NSArray
而不是NSDictionary
,这样它才能工作。