objective-c - 如何用 ASIHttpRequest JSON 数据填充 UITableView?

标签 objective-c ios json uitableview asihttprequest

我正在尝试用 this JSON data 填充我的 UITableView使用 ASIhttprequest,但我得到以下异常:__NSArrayM objectForKey:]: unrecognized selector sent to instance

我在 Controller 中使用以下代码来实现此目的:

#import <UIKit/UIKit.h>

@interface tableListViewController : UITableViewController {
    NSDictionary *data;
}

@end

和主文件:

#import "tableListViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@interface tableListViewController ()

@end

@implementation tableListViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"http://api.glennzo.nl/glow/index.json"];
    ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setTag:100];
    [request setDelegate:self];
    [request startSynchronous];

    //Set the title
    self.navigationItem.title = @"Events";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void) requestFinished:(ASIHTTPRequest *) request
{
    if (request.tag == 100) {
        NSData *responseData = [request responseData];
        NSError *error;

        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];

        if(!JSON || ![JSON count])
        {
            NSLog(@"%@", @"geen correcte json data van request gekregen");
        }
        else
        {
            data = JSON;
        }
    }
}

- (void) requestFailed:(ASIHTTPRequest *) request
{
    NSError *error = [request error];
    NSLog(@"Error: %@",error.localizedDescription);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [data count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [UITableViewCell alloc];
    }

    NSArray *namen = [data objectForKey:@"name"];
    cell.textLabel.text = [namen objectAtIndex:indexPath.row];

    return cell;
}

感谢一些调试,我现在知道 ASIHttpRequest 可以工作,但是我创建的 NSdictionary(并打算在整个应用程序周围使用)给我带来了麻烦。有人可以让我朝着正确的方向前进吗?

最佳答案

错误似乎在:

NSArray *namen = [data objectForKey:@"name"];

所以我认为 data 类型不正确(NSDictionary),为 nil 或者它被其他类型填充:

[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];

尝试查看通过 JSON 到达的内容。例如:

NSError *jsonError = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError];

if ([jsonObject isKindOfClass:[NSArray class]]) {
    NSLog(@"its an array!");
    NSArray *jsonArray = (NSArray *)jsonObject;
    NSLog(@"jsonArray - %@",jsonArray);
    } else {
    NSLog(@"its probably a dictionary");
    NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
    NSLog(@"jsonDictionary - %@",jsonDictionary);
}

关于objective-c - 如何用 ASIHttpRequest JSON 数据填充 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12590841/

相关文章:

objective-c - 我可以在单独的线程上构建 UIView 元素吗?

ios - 无法验证下载服务器。您可能正在遭受中间人攻击

ios - react-native run-ios 不断失败

json - Azure 数据资源管理器流摄取格式化的 JSON 文档

javascript - 从数组中检索对象,其中另一个对象与另一个数组匹配

mysql - Rails 与 mysql 无法保存 json 数据类型

iphone - UITableView 在编辑后取消选择选定的单元格

php - 如何从ios正确上传大文件?

iOS 和 RestKit : How to get a text/html response right?

ios - 如何在不阻塞 UI 的情况下创建 UIView