[__NSCFDictionary objectAtIndex:]: unrecognized selector geschickt Instanz
Ich versuche zu Parsen einer Json-Datei in der Tabelle angezeigt und ich bin immer diese Fehlermeldung
[__NSCFDictionary objectAtIndex:]: unrecognized selector geschickt Instanz
und die app zum Absturz zu bringen. Bitte helfen Sie mir, ich bin neu in der iOS-Entwicklung.
Mein 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;
}
- Das problem ist, dass Sie erwarten, dass Ihre Daten in ein array, aber in der Realität ist ein Wörterbuch, und das ist, warum ist es abstürzt. Überprüfen Sie Ihre
feed
Objekt...
Du musst angemeldet sein, um einen Kommentar abzugeben.
Das problem ist, dass
feed
ist nicht einNSArray
, aber einNSDictionary
.Blick auf die JSON, werden Sie wahrscheinlich wollen, um Zugriff auf diesen array:
[feed objectForKey:@"entry"]
Die top-level-Objekt in Ihrem-feed ist ein JSON-Objekt, nicht ein JSON-array. Also die deserialisation gibt Ihnen ein NSDictionary, nicht ein NSArray.