Reputation: 354
I'm making an API Calls which contain live information. This can 2 types of information. If there is information it will return something like this:
goaltype = "Regular goal";
id = 2787821;
minute = 45;
player = Nono;
"player_id" = 317569;
playershort = Nono;
team = "Real Betis";
"team_id" = 8603;
If there is no information it will return:
error = "no live games found"
If it returns error my tableview will at the moment count the error message as 1 object and return 1 cell. So if the results return error, then it should not run the loop. How can i obtain this or is there another way?
my code:
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]];
}
Upvotes: 0
Views: 71
Reputation: 4447
What NeverHopeless said… plus… (much simpler syntax)
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]
];
}
}
(I would have posted this as a comment on NeverHopeless's answer except that comments lose formatting.)
Upvotes: 0
Reputation: 11233
If no sight of error
key that means you are getting expected data, so iterating on resultset is expected to be safe.
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]];
}
}
Upvotes: 3