2013-03-20 66 views
1

我试图分析JSON文件到表视图,我收到此错误[__NSCFDictionary objectAtIndex:]:无法识别的选择发送到实例

[__NSCFDictionary objectAtIndex:]:无法识别的选择发送到实例 和应用程序崩溃。请帮助我,我是iOS开发新手。

我的代码

@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; 
} 
+1

问题是,你期望你的数据是一个数组,但实际上是一本字典,这就是为什么崩溃。检查你的'饲料'对象... – Alladinian 2013-03-21 15:51:27

回答

8

的问题是,feed不是NSArray,但NSDictionary

望着JSON,你可能要访问这个数组:[feed objectForKey:@"entry"]

3

顶级对象Feed中的是一个JSON对象,而不是一个JSON阵列。所以反序列化给你一个NSDictionary,而不是NSArray。

+0

现在的应用程序不崩溃,但我gettina空白表视图..呵呵.... – user2192724 2013-03-21 19:40:05

+3

@ user2192724 - 好!听起来你已经取得了很大的进步! (提示:NSLog) – 2013-03-28 02:32:27

相关问题