将字典应用于IOS中的表视图时出错

时间:2014-10-16 17:06:49

标签: ios objective-c uitableview nsdictionary

我非常擅长使用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;
}

我在这里设置self.data:

// 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];

1 个答案:

答案 0 :(得分:0)

self.data必须属于NSArray类型,而不是NSDictionary,然后才能生效。