我正在从 Instagram JSON API 获取用户 ID。
它只返回一个值作为 Dictionary valueForKeyPath。
我如何将它转换为字符串。我需要在 Instagram 上使用用户 ID 进行一些操作。
我试过这样转换
NSString *userId = [NSString stringWithFormat:@"%@", [dictionary valueForKeyPath:@"id"]];
URL 不是字符串,我无法使用如下 url 重新连接到 Instagram:
URL: https://api.instagram.com/v1/users/( 123456 )/media/recent?access_token=567890.asasasddsdsdsdsdsadsdsdasd
最佳答案
我认为您可以尝试从数组中获取值。我猜它在索引 0
NSString *userId = [NSString stringWithFormat:@"%@", [dictionary valueForKeyPath:@"id"][0]];
或
NSString *userId = [NSString stringWithFormat:@"%@", [[dictionary valueForKeyPath:@"id"] objectAtIndex:0]];
因为如果数组为 nil,程序将因第一个代码而崩溃。让我知道它是否有效
正如@Paulw11 所说,您不需要 stringWithFormat 因为字符串在数组中 代码行应该是
NSString *userId = [[dictionary valueForKeyPath:@"id"] objectAtIndex:0];
编码愉快!
关于ios - 获取 NSDictionary 值作为 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24580621/