NSMutable Array Property becomes null after function is exited
I am trying take the values of an array of CLLocation's made from a JSON
request and assign them to a property that is a NSMutable array. Here the
relevant controller code:
- (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];
}
How come this array become's null in viewDidLoad?
No comments:
Post a Comment