结合NSDictionaries

时间:2012-11-14 13:47:50

标签: objective-c ios json cocoa-touch

目标:将返回的所有NSDictionaries与一个公共键值'delay'合并为一个NSMutablearry,我可以用它来比较结果(true或false),在地图上放置绿色或红色图钉。
此代码以JSON的形式返回机场数据,我将其转换为所请求机场的字典。有许多键,但在这部分代码中,我只对一个 - '延迟'感兴趣,我希望将它组合成一个NSMutablearry。
因为我一次只能申请一个机场,所以它会单独收回每个要求的机场,我会获得9组数据。我想要的是9个不同机场的一个阵列中的9个延迟键。

`- (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`

1 个答案:

答案 0 :(得分:1)

以下是您的问题:每次创建新词典时,您都要实例化airportDelays数组,因此,您只需在销毁airportDelay阵列并创建新词典之前添加一个词典。见下文

self.airportDelays = [[NSMutableArray alloc] initWithCapacity:9];


[self parseDelay:dictionary];

应该是

if (!self.airportDelays) {
    self.airportDelays = [[NSMutableArray alloc] initWithCapacity:9];
}
[self parseDelay:dictionary];

我也厌倦了使用for循环来进行网络操作。您可能应该使用不同的排队机制,程序在向Web服务请求下一个之前等待第一个字典的响应。不过,这可能是另一个不同时期的教训。

祝你好运