1

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でnullになるのはなぜですか?

4

2 に答える 2

5

メソッドからデータを取得する非同期リクエストを行うためにGCDを使用していますviewDidLoadviewDidLoad非同期であるため、メソッドをブロックしません。非同期リクエストがダウンロードされて解析されると、配列に値が入力されます。それがあなたの配列がnilにある理由viewDidLoadです。

データがダウンロードされて配列に入力されるまで UI が空白に見える場合は、アクティビティ インジケーターを表示することを選択できます。これにより、アプリのユーザーは何らかのアクティビティが行われていることがわかります。

それが役立つことを願っています!

于 2013-09-06T06:31:31.463 に答える
0

おそらくあなたがする必要があるのは、プロセスが完了するのを待つことであり、ブロックは文字通りあなたがその中で何をしているのかわからないか、内部関数がブロックによってキャプチャされていないと言うでしょう。あなたの配列を使用している)ブロックが完了した後にのみ。

- (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);

}
于 2013-09-06T06:44:39.120 に答える