xcode [__NSCFDictionary objectAtIndex:]:发送到实例的无法识别的选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15534504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance
提问by user2192724
I am trying to parse a Json file into the Table view and I am getting this error
我正在尝试将 Json 文件解析到表视图中,但出现此错误
[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance and the app is crashing. Please help me, I am new to iOS development.
[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例,应用程序崩溃。请帮助我,我是 iOS 开发的新手。
My Code
我的代码
@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;
}
回答by tilo
The problem is that feed
is not a NSArray
, but a NSDictionary
.
问题是这feed
不是一个NSArray
,而是一个NSDictionary
。
Looking at the JSON, you likely want to access this array: [feed objectForKey:@"entry"]
查看 JSON,您可能想要访问此数组: [feed objectForKey:@"entry"]
回答by JeremyP
The top level object in your feed is a JSON object, not a JSON array. So the deserialisation gives you an NSDictionary, not an NSArray.
您的提要中的顶级对象是一个 JSON 对象,而不是一个 JSON 数组。所以反序列化给你一个 NSDictionary,而不是一个 NSArray。