[__NSCFDictionary objectAtIndex:]:无法识别的选择器已发送到实例 [英] [__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance

查看:66
本文介绍了[__NSCFDictionary objectAtIndex:]:无法识别的选择器已发送到实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Json文件解析为Table视图,但出现此错误

I am trying to parse a Json file into the Table view and I am getting this error

[__ NSCFDictionary objectAtIndex:]:无法识别的选择器已发送到实例 并且该应用崩溃了.请帮助我,我是iOS开发的新手.

[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance and the app is crashing. Please help me, I am new to iOS development.

我的代码

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
}
    return self;
}

- (void)viewDidLoad
{
    self.title = @"Feeds";
    [super viewDidLoad];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL *url = [NSURL URLWithString:@"http://little-people.blogspot.com/feeds/posts        /default?alt=json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
    NSLog(@"Data Data , %@", data);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    feed = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"Data  , %@", feed);
    [mainTableView reloadData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download   could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}    


- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [feed count];
    NSLog(@"Data Data , %@", feed);
 }
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    if(cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
}
if (([feed count] - 1) >=indexPath.row) {

    cell.textLabel.text = [[feed objectAtIndex:indexPath.row] objectForKey:@"feed"];
    cell.detailTextLabel.text = [[feed objectAtIndex:indexPath.row] objectForKey:@"title"];
}



    return cell;
}

推荐答案

问题是feed不是NSArray,而是NSDictionary.

查看JSON,您可能要访问此数组:[feed objectForKey:@"entry"]

Looking at the JSON, you likely want to access this array: [feed objectForKey:@"entry"]

这篇关于[__NSCFDictionary objectAtIndex:]:无法识别的选择器已发送到实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆