Search code examples
objective-ciosnsdictionarynsnumber

How to access data in a NSDictionary with objectForKey


I'm trying to access the data in a dictionary. I'm able to access the data when it's a string like:

cell.textLabel.text = [[item objectForKey:@"merchant"]objectForKey:@"name"];

but am not sure about the syntax for an NSNumber:

NSNumber* latFromTable = [[displayItems objectAtIndex:indexPath.row]
                                           [objectForKey:@"coords"]objectForKey:@"lat"];

I know I'm using NSStrings in the code above which won't work. How would I change that to work with NSNumber?

the data in the dictionary is presented as the following. The objectForKey is "coords". I want access to item 1 and 2 (lat and lng)

 "coords": {
        "lat": 52.5032,
        "lng": 13.3445

thanks for any help.


Solution

  • The Issue with the following code is mismatching of brackets:

    NSNumber* latFromTable = [[displayItems objectAtIndex:indexPath.row]
                                               [objectForKey:@"coords"]objectForKey:@"lat"];
    

    So change it like:

    float latFromTable = (float)[[[displayItems objectAtIndex:indexPath.row]
                                               objectForKey:@"coords"]objectForKey:@"lat"];
    NSNumber *value = [NSNumber numberWithFloat:latFromTable];