0
我正在进行API调用以获取一些实时信息。当没有实时信息时,它将返回:如果在API请求中返回错误消息,则不要开始循环
error =“没有找到真人游戏”;
否则它会返回不同的信息,如hometeam,awayteam,score等。
在我的循环中,我将这些信息添加到数组arrayEuropa中。问题是如果api请求返回错误消息,它不应该向arrayEuropa添加任何东西。
我该如何获得这个?
我试过不同的事情,当它返回错误信息,但没有错误信息时崩溃。
这里是我的代码:
NSDictionary* headers4 = @{@"X-Mashape-Authorization": @"key"};
NSDictionary* parameters4 = @{};
UNIHTTPJsonResponse* response4 = [[UNIRest get:^(UNISimpleRequest* request4) {
[request4 setUrl:@"https://willjw-statsfc-competitions.p.mashape.com/live.json?key=APIKEY&competition=europa-league&timezone=Europe%2FLondon"];
[request4 setHeaders:headers4];
[request4 setParameters:parameters4];
}] asJson];
NSData* rawBody4 = [response4 rawBody];
results4 = [NSJSONSerialization JSONObjectWithData:rawBody4 options:NSJSONReadingMutableContainers error:nil];
for (int i = 0; i <= results4.count-1; i++)
{
NSString *homeTeam = [[results4 valueForKey:@"homeshort"] objectAtIndex:i ];
NSString *awayTeam = [[results4 valueForKey:@"awayshort"] objectAtIndex:i ];
NSString *time = [[results4 valueForKey:@"statusshort"] objectAtIndex:i ];
NSString *homeScore = [NSString stringWithFormat:@"%@", [[[results4 valueForKey:@"runningscore"] objectAtIndex:i ] objectAtIndex:0]];
NSString *awayScore = [NSString stringWithFormat:@"%@", [[[results4 valueForKey:@"runningscore"] objectAtIndex:i ] objectAtIndex:1]];
[arrayEuropa addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:homeTeam,@"hometeam", awayTeam,@"awayteam", time, @"time", homeScore, @"homescore", awayScore, @"awayscore", nil]];
}
你在哪里检查响应中的错误字段? –
我现在不检查if(results [@“error”]),但不能正常工作 – user3258468
您可以记录results4的内容以获得成功和错误,并将其添加到问题中。 – Wain