[__NSCFDictionary objectAtIndex:]:发送到实例的无法识别的选择器 [英] [__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance
本文介绍了[__NSCFDictionary objectAtIndex:]:发送到实例的无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将 Json 文件解析到表格视图中,但出现此错误
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
>.
The problem is that feed
is not a NSArray
, but a NSDictionary
.
查看 JSON,您可能想要访问此数组:[feed objectForKey:@"entry"]
Looking at the JSON, you likely want to access this array: [feed objectForKey:@"entry"]
这篇关于[__NSCFDictionary objectAtIndex:]:发送到实例的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文