2014-02-27 34 views
0

我正在進行包含實時信息的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]]; 

} 

回答

0

什麼NeverHopeless說... ...加...(更簡單的語法)

if (![result valueForKey:@"error"]) { 
    for (int i = 0; i < results.count; i++) { 
     NSString *homeTeam = results[@"home short"][i]; 
     NSString *awayTeam = results[@"awayshort"][i]; 
     NSString *time = results[@"statusshort"][i]; 
     NSString *homeScore = [NSString stringWithFormat:@"%@", results[@"runningscore"][i][0]; 
     NSString *awayScore = [NSString stringWithFormat:@"%@", results[@"runningscore"][i][1]; 
     [arrayBarclay addObject:[NSMutableDictionary dictionaryWithDictionary:@[ 
      @"hometeam":homeTeam, 
      @"awayteam":awayTeam, 
      @"time":time, 
      @"homescore":homeScore, 
      @"awayscore":awayScore] 
     ]; 
    } 
} 

(我會發布此作爲NeverHopeless的回答評論只是評論失去格式。)

3

如果沒有看見error關鍵的,這意味着你得到預期的數據,所以在遍歷ResultSet的預期是安全的。

if(![result valueForKey:@"error"]) 
{ 
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]]; 

} 
} 
+0

那纔怪! [結果valueForKey:@「錯誤」]不會觸發,並且沒有鍵稱爲錯誤 – user3258468

+0

您的問題是否解決? – NeverHopeless

相關問題