Search code examples
iosjsonobjective-cinstagram

get NSDictionary value as NSString


I am fetching user-id from Instagram JSON API.

It return only one value as Dictionary valueForKeyPath.

How I could convert it to string. I need to use the userid with some operations on Instagram.

I tried to convert it by this way:

NSString *userId = [NSString stringWithFormat:@"%@", [dictionary valueForKeyPath:@"id"]];

The URL is coming not like string and I can't reconnect to Instagram with url like below:

URL: https://api.instagram.com/v1/users/( 123456 )/media/recent?access_token=567890.asasasddsdsdsdsdsadsdsdasd


Solution

  • I think you can try to get the value from the array. I guess it at index 0

    NSString *userId = [NSString stringWithFormat:@"%@", [dictionary valueForKeyPath:@"id"][0]];
    

    or

    NSString *userId = [NSString stringWithFormat:@"%@", [[dictionary valueForKeyPath:@"id"] objectAtIndex:0]];
    

    because if the array is nil the program will crash with the first code. Let me know if it worked

    As @Paulw11 said you don't need the stringWithFormat because string is in the array the line of code should be

    NSString *userId = [[dictionary valueForKeyPath:@"id"] objectAtIndex:0];
    

    Happy Coding!