Reputation: 17
Goal: Combine all NSDictionaries returned with one common key value 'delay' into a single NSMutablearry that I can use to compare the results (true or false) to place either a green or red pin on a map.
This code brings back the airport data in JSON that I convert to a dictionary of the requested airports. There are a number of keys, but in this part of the code I am only interested in one - 'delay' that I want to combine into one NSMutablearry.
Because I can only request one airport at a time, it brings back each requested airport individually and I get 9 sets of data. What I want is the 9 delay keys all in one array for the 9 different airports.
`- (void)configureData
{
self.airportCodes = [[NSArray alloc] initWithObjects:
@"ATL",
@"BOS",
@"BWI",
@"CLT",
@"CVG",
@"DEN",
@"EWR",
@"ORD",
@"SFO",
nil];
NSUInteger airportCount = self.airportCodes.count;
for(int i=0; i < airportCount;i++){
NSURL *url = [self urlWithSearchText:[self.airportCodes objectAtIndex: i]];
NSString *jsonString = [self performAirportRequestWithURL:url];
if (jsonString == nil) {
[self showNetworkError];
}
NSDictionary *dictionary = [self parseJSON:jsonString];
if (dictionary == nil) {
[self showNetworkError];
}
self.airportDelays = [[NSMutableArray alloc] initWithCapacity:9];
[self parseDelay:dictionary];
//NSLog(@"AP Delays %@",self.airportDelays);
}
return;
}
- (void)parseDelay:(NSDictionary *)dictionary
{
//self.delay = [dictionary objectForKey:@"delay"];
[self.airportDelays addObject:dictionary];
NSLog(@"AP Delays %@",self.airportDelays);
return;
}
- (NSURL *)urlWithSearchText:(NSString *)searchText
{
NSString *escapedSearchText =
[searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString =
[NSString stringWithFormat:
@"http://services.faa.gov/airport/status/%@? format=application/json", escapedSearchText];
NSURL *url = [NSURL URLWithString:urlString];
return url;
}
- (NSString *)performAirportRequestWithURL:(NSURL *)url
{
NSError *error;
NSString *resultString = [NSString stringWithContentsOfURL:
url encoding:NSUTF8StringEncoding error:&error];
if (resultString == nil) {
NSLog(@"Download Error: %@", error);
return nil;
}
return resultString;
}
- (void)showNetworkError
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Whoops..."
message:@"There was an error reading
from the FAA Server. Please try again."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
- (NSDictionary *)parseJSON:(NSString *)jsonString
{
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
id resultObject = [NSJSONSerialization JSONObjectWithData:data options:
kNilOptions error:&error];
if (resultObject == nil) {
NSLog(@"JSON Error: %@", error);
return nil;
if (![resultObject isKindOfClass:[NSDictionary class]]) {
NSLog(@"JSON Error: Expected dictionary");
return nil;
}
}
return resultObject;
}
@end`
Upvotes: 0
Views: 256
Reputation: 9035
Here is your issue: You are instantiating the airportDelays array every time a new dictionary is created, therefore, you are only adding one dictionary before destroying the airportDelay array and creating a new one. See below
self.airportDelays = [[NSMutableArray alloc] initWithCapacity:9];
[self parseDelay:dictionary];
Should be
if (!self.airportDelays) {
self.airportDelays = [[NSMutableArray alloc] initWithCapacity:9];
}
[self parseDelay:dictionary];
I'm also weary of you using a for loop to do your network operations. You should probably be using a different queueing mechanism, where the program waits for the response for the first dictionary before asking the webservice for the next. That might be a different lesson for a different time though.
Good luck
Upvotes: 1