Returned JSON data is changing from NSDictionary to NSArray

107 Views Asked by At

I'm having a little difficulty with a JSON service that I'm consuming and iterating over. When I consume the service I am looping over the data as you would expect because of the number of records.

I'm saving that loop'ed data into an NSArray which I use later in a UITableView. Next I'm simply allowing the user to tap the selected row (from the json data result) to show more detail. Pretty simple so far.

Every element from the JSON service is NSString. So far nothing tricky. However, one element within the NSArray after the service has been put into the NSObject is showing HEX code, see below.

altitude    NSString *  0x7ff8d4cd3d30  0x00007ff8d4cd3d30

Of course the app has a meltdown because it can't figure out what HEX is when I'm using that NSArray object to display key elements i.e. altitude. Now the odd thing is every other element within the NSArray looks like this see below.

latitude    __NSCFString *  @"21.45852" 0x00007ff8d4ca54f0

I have read a few suggestions stating this is normal for NSString and JSON data. But not really how to fix it.

What I have found is that NSArray after the JSON is complete is changing just that one element. I have also tried changing it from an INT to an NSString however same result (I know its a NSString in the first place btw, I was just trying different ideas.)

Abstract of JSON Call and loop to add into NSArray object.

    //Do something with returned array
               dispatch_async(dispatch_get_main_queue(), ^{
                   NSDictionary *pilotJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

                   //Loop through the JSON array
                   NSArray *currentPilotsArray = [pilotJson valueForKeyPath:@""];

                   //set up array and json call
                   pilotsArray = [[NSMutableArray alloc]init];

                   NSArray *keys=[pilotJson allKeys];
                   for (NSString *key in keys){
                       NSDictionary *elementDictionary=pilotJson[key];

                       NSString *altitude = elementDictionary[@"altitude"];

                       NSInteger n = [altitude intValue];
                       NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                       formatter.numberStyle = NSNumberFormatterDecimalStyle;
                       NSString *string = [formatter stringFromNumber:@(n)];
                       NSString *nAltitude = [NSString stringWithFormat:@"%@ ft", string];

[pilotsArray addObject:[[LiveMap alloc]initWithaltitude:nAltitude ]];
.
.
.

So when I get to this point of the code where the user taps the relevant record I get a crash and the application aborts. I'm assuming this is from the above NSString vs __NSCFString

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Pass the details the the detail view controller
    PilotsFlightDetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"FlightDetails"];

   NSLog(@"array: %@", pilotsArray);
    NSString *AltitudeString = [[self.pilotsArray valueForKey:@"altitude"]objectAtIndex:indexPath.row];    <-----WIGS OUT HERE

I find this super odd as every other element works normally, but this one simply has issues. Any suggestions?

UPDATE: NSLog of pilotsArray as per the request.

[1] LiveMap *   0x7f8b0265e1b0  0x00007f8b0265e1b0
altitude    __NSCFString *  @"21 ft"    0x00007f8b02664860

Also the jsonArray from the Service Directly.

enter image description here

1

There are 1 best solutions below

0
On

Okay. I got it working. There was nothing wrong with the code. It however recognised another NSObject that had a same name "altitude" and for some reason it was getting mixed up.

I changed the name in the NSObject to something entirely unique and updated the instances in the relevant places. This did it. Lesson learnt always make sure you have named your variables appropriately.