0

Parsing a JSON file saved locally,storing JSON data in NSArray and further NSArray storing in id type to print values in table view but getting crash in getting int value like "id" also what I have to do if I wants to display different values in custom TableViewCell.


  -(void)jasonParsing
{
    
    NSString * filePath =[[NSBundle mainBundle] pathForResource:@"EmployeeData" ofType:@"json"];

        NSError * error;
        NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];


        if(error)
        {
            NSLog(@"Error reading file: %@",error.localizedDescription);
        }

//@property NSArray *dataList; defined in (.m)
        self.dataList = (NSArray *)[NSJSONSerialization
                                    JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                                    options:0 error:NULL];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
   

    if (cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    id keyValuePair =self.dataList[indexPath.row];
//by using below code crashing with error([__NSCFNumber isEqualToString:]: unrecognized selector sent to instance)
    cell.textLabel.text = keyValuePair[@"id"];
//by using below code values are printing all the name
    cell.textLabel.text = keyValuePair[@"name"];


        
        return cell;
    
}
3
  • eyValuePair[@"id"]; is a NSNumber, not a NSString according to the error. You can do [keyValuePair[@"id"] stringValue]. Commented May 17, 2022 at 16:37
  • Thanks a lot for a quick response,Yes it worked, can We call it a type casting? and thanks once again. Commented May 17, 2022 at 16:45
  • It's not casting. Casting is what you did (NSArray *), ie: tell the compiler don't worry, you know it's id, BUT i know I'm telling you it's an NSArray, so treat it as such. Here, you are "converting". NSNumber has a method that return a NSString with its own value. It's really a NSString. Commented May 17, 2022 at 16:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.