退出函数后,NSMutableArray属性变为null

时间:2013-09-06 06:24:55

标签: ios objective-c json

我正在尝试从JSON请求中获取CLLocation数组的值,并将它们分配给属于NSMutable数组的属性。 这里是相关的控制器代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //using a background thread to receive the json call so that the UI doesn't stall
    dispatch_async(directionQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:openMapURL];
        [self performSelectorOnMainThread:@selector(fetchedData:)
                               withObject:data waitUntilDone:YES];
    });

    NSLog(@"%@", self.coordinates);
}

- (void)fetchedData:(NSData *)responseData 
{
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData //turn the data from the json request into a gigantic dictionary
                                     options:0
                                       error:&error];

    NSDictionary* route = [json objectForKey:@"route"]; //created a dictionary out of the contents of route
    NSDictionary* shape = [route objectForKey:@"shape"]; //create a sub-dictionary with the contents of shape
    NSArray* shapePoints = [shape objectForKey:@"shapePoints"]; //turn shapePoints object into an NSArray

    //Loop to turn the array of coordinate strings into CLLocation array
    NSMutableArray* locations = [[NSMutableArray alloc] init];
    NSUInteger i;
    for (i = 0; i < ([shapePoints count])/2 ; i = i+2) 
    {
        [locations addObject: [[CLLocation alloc]
                               initWithLatitude:[[shapePoints objectAtIndex:i] floatValue]
                               longitude:[[shapePoints objectAtIndex:i+1] floatValue]
                               ]];
    }

    //When I NSLog within the function, the array has the correct data. But in viewDidLoad
    //the array is null
    [self.coordinates initWithArray:locations copyItems:YES];   
}

为什么这个数组在viewDidLoad中变为空?

2 个答案:

答案 0 :(得分:5)

您正在使用GCD发出异步请求以从viewDidLoad方法获取数据。由于它是异步的,因此不会阻止viewDidLoad方法。一旦异步请求下载并被解析,该数组就会被填充。这就是您的数组在nilviewDidLoad的原因。

如果您的UI在数据下载并填充数组之前看起来空白,您可以选择显示活动指示器。这将使应用程序用户了解某些活动正在进行中。

希望有所帮助!

答案 1 :(得分:0)

你可能需要做的是等待进程完成,并且块实际上不知道你在里面做了什么,或者我会说内部函数没有被block捕获。所以使用数组(只有在块完成后才调用正在使用你的数组的函数。

- (void)fetchedData:(NSData *)responseData 
{
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData //turn the data from the json request into a gigantic dictionary
                          options:0
                          error:&error];

    NSDictionary* route = [json objectForKey:@"route"]; //created a dictionary out of the contents of route
    NSDictionary* shape = [route objectForKey:@"shape"]; //create a sub-dictionary with the contents of shape
    NSArray* shapePoints = [shape objectForKey:@"shapePoints"]; //turn shapePoints object into an NSArray

    //Loop to turn the array of coordinate strings into CLLocation array
    NSMutableArray* locations = [[NSMutableArray alloc] init];
    NSUInteger i;
    for (i = 0; i < ([shapePoints count])/2 ; i = i+2) 
    {
        [locations addObject: [[CLLocation alloc]
                               initWithLatitude:[[shapePoints objectAtIndex:i] floatValue]
                               longitude:[[shapePoints objectAtIndex:i+1] floatValue]
                               ]];
    }

    //When I NSLog within the function, the array has the correct data. But in viewDidLoad
    //the array is null

    [self.coordinates initWithArray:locations copyItems:YES];

// 在这里,您可以调用访问self.coordinates数组的方法。

NSLog(@"%@", self.coordinates);

}