2013-03-20 39 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

相關問題