检查valueForKey是否存在

时间:2014-02-23 21:03:11

标签: ios objective-c request

我正在发出请求,有时可能会返回错误消息示例错误:“没有实时游戏”。

如何检查valueforkey:错误是否存在?

if (error exists) {
 //nothing
} else {
 //return other value for keys
}

我的要求

NSDictionary* headers3 = @{@"X-Mashape-Authorization": @"key"};
NSDictionary* parameters3 = @{};

UNIHTTPJsonResponse* response3 = [[UNIRest get:^(UNISimpleRequest* request3) {
    [request3 setUrl:@"https://willjw-statsfc-competitions.p.mashape.com/live.json?key=uNzRmnUdCEYKdOoe1e1bBpwtmczNVLUZYbIlOX9O&competition=europa-league&timezone=Europe%2FLondon"];

    [request3 setHeaders:headers3];
    [request3 setParameters:parameters3];
}] asJson];


NSData* rawBody3 = [response3 rawBody];
results3 = [NSJSONSerialization JSONObjectWithData:rawBody3 options:NSJSONReadingMutableContainers error:nil];

1 个答案:

答案 0 :(得分:0)

似乎您的API返回“消息”而不是“错误”。无论如何,这就是你做的:

if (result3[@"message"]) { // equavalent of [result3 objectForKey:@"message"], which will return nil if there's no "message" key in the dictionary
  // your error handler condition
}
else {
  // process retrieved data
}