我正在尝试检查我的JSON响应字典中是否存在“next_max_id”这个键的对象。
我已经尝试过以下几种方法进行检查:
if ([[pagination objectForKey:@"next_max_id"] isKindOfClass:[NSNull class]]) ...
if ([pagination objectForKey:@"next_max_id"] == [NSNull class]) ...
if ([pagination objectForKey:@"next_max_id"]) ...
pagination = {};
),我会收到以下错误消息:-[NSNull objectForKey:]: unrecognized selector sent to instance ...
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKey:]: unrecognized selector sent to instance ...
如何检查对象或键是否存在,而不会导致崩溃?
更新:
当键next_max_id
存在于pagination
中时,JSON响应将如下所示
"pagination": {
"next_url": "https://api.instagram.com/v1/tags/puppy/media/recent?access_token=fb2e77d.47a0479900504cb3ab4a1f626d174d2d&max_id=13872296",
"next_max_id": "13872296"
}
next_max_id
将不存在。JSON 响应将会是这样的pagination = {};
。 - Scott