我正在進行包含實時信息的API調用。這可以有2種類型的信息。如果沒有信息將返回是這樣的:檢查NSMutableDictionary的值
goaltype = "Regular goal";
id = 2787821;
minute = 45;
player = Nono;
"player_id" = 317569;
playershort = Nono;
team = "Real Betis";
"team_id" = 8603;
如果沒有信息將返回:
error = "no live games found"
如果返回錯誤我的tableview將在瞬間計算錯誤消息1對象並返回1個單元格。所以如果結果返回錯誤,那麼它不應該運行循環。我如何獲得這個或有另一種方式?
我的代碼:
NSDictionary* headers = @{@"X-Mashape-Authorization": @"MASHAPEKEY"};
NSDictionary* parameters = @{};
UNIHTTPJsonResponse* response = [[UNIRest get:^(UNISimpleRequest* request) {
[request setUrl:@"https://willjw-statsfc-competitions.p.mashape.com/live.json?key=APIKEY&competition=premier-league&timezone=Europe%2FLondon"];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJson];
NSData* rawBody = [response rawBody];
results = [NSJSONSerialization JSONObjectWithData:rawBody options:NSJSONReadingMutableContainers error:nil];
for (int i = 0; i <= results.count-1; i++)
{
NSString *homeTeam = [[results valueForKey:@"homeshort"] objectAtIndex:i ];
NSString *awayTeam = [[results valueForKey:@"awayshort"] objectAtIndex:i ];
NSString *time = [[results valueForKey:@"statusshort"] objectAtIndex:i ];
NSString *homeScore = [NSString stringWithFormat:@"%@", [[[results valueForKey:@"runningscore"] objectAtIndex:i ] objectAtIndex:0]];
NSString *awayScore = [NSString stringWithFormat:@"%@", [[[results valueForKey:@"runningscore"] objectAtIndex:i ] objectAtIndex:1]];
[arrayBarclay addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:homeTeam,@"hometeam", awayTeam,@"awayteam", time, @"time", homeScore, @"homescore", awayScore, @"awayscore", nil]];
}
那纔怪! [結果valueForKey:@「錯誤」]不會觸發,並且沒有鍵稱爲錯誤 – user3258468
您的問題是否解決? – NeverHopeless