-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8943940 while parsing JSON Page to UITableView

大城市里の小女人 提交于 2019-12-04 04:58:14

问题


I have been through many links and fortunately I got many similar links but nothing worked Out.

my array is appearing like this in Output, using NSLog()..

products =     (
            {
        cid = 1;
        name = "hello world";
    },
            {
        cid = 2;
        name = "It is an array";
    },
            {
        cid = 3;
        name = "error error";
    },
            {
        cid = 4;
        name = "OMG! still same";
    },
            {
        cid = 5;
        name = "any help";

  ...
  ...
   },
            {
        cid = 130;
        name = "How is the work going on.";
    },
            {
        cid = 131;
        name = "Had a nice lunch";
    }
);
success = 1

Code to parse JSON, where "news" is an Array var..

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
    news = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"Feeds...%@",news);
    [myTableView reloadData];
}

Code to display table row, here in "news" value is being displayed by NSLog(); Error comes for NSDictionary.

-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   NSLog(@"=====%@====",news);
   NSDictionary *myDict = [news objectAtIndex:indexPath.row];
   NSArray *myArray = [myDict objectForKey:@"name"];
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
   if (cell==nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}

for (NSString *str in myArray) {
    cell.textLabel.text = str;
}
   return cell;

}

I am not able to find what i'm missing here.., Please help me out.

Thank you in advance.


回答1:


Your JSON structure is something like this:

Dictionary-->Array-->Object (Dictionary).

So you need to do:

  1. Extract the Array from the dictionary using the key product
  2. Extract an object(Dictionary) from the array using the index
  3. Get the name from the dictionary object using the key name

Instead of this:

NSDictionary *myDict = [news objectAtIndex:indexPath.row];
NSArray *myArray = [myDict objectForKey:@"name"];

Use:

NSArray *myArr = [news objectForKey:@"products"];
cell.textLabel.text = [[myArr objectAtIndex:0] objectForKey:@"name"];

Edit

Also change your numberOfRowsInSection like:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[news objectForKey:@"products"] count];
}



回答2:


news is a NSDictionary, and a dictionary does not have a method objectAtIndex:
You have to use instead [news objectForKey:...];




回答3:


[news objectAtIndex:indexPath.row];

NSMutabledictionary is indexpath.row so add first news in array and that array name add in your code and you get nothing an




回答4:


Put NSDictionary in Array then use it in Table method

 NSMutableArray *result=[[NSMutableArray alloc] init];
  result = [googleResponse valueForKey: @"products"];


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *dt = [result objectAtIndex:indexPath.row];

}  

It will Help you !!!!!



来源:https://stackoverflow.com/questions/16521692/nscfdictionary-objectatindex-unrecognized-selector-sent-to-instance-0x894

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!