Tuesday, 10 September 2013

Dealing with nested NSDictionary keys

Dealing with nested NSDictionary keys

I need to grab a set of latitude and longitude points and use them to plot
map markers, but for some reason I cannot store them as a double value and
pass them into my method. I have tried logging out the the values using
this:
// Make the URL connection
NSString *urlAsString = @"...........";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// Error and success message handling.
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError
*error) {
if([data length] > 0 &&
error == nil){
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if (jsonData != nil){
NSError *error = nil;
self.result = [NSJSONSerialization
JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers error:&error];
if(error == nil)
NSLog(@"Longitude: %@",[_result
valueForKeyPath:@"merchants.branches.longitude"]);
NSLog(@"Name: %@", [_result
valueForKeyPath:@"merchants.branches.name"]);
}
}
else if ([data length] == 0 &&
error == nil){
NSLog(@"Nothing was downloaded");
}
else if (error != nil){
NSLog(@"Error happened = %@", error);
}
}];
}
and this is the data that is sent back:
2013-09-10 10:14:35.775 JoySync[597:5803] Longitude: (
(
"-115.118958"
),
(
"-114.985397"
),
(
"-115.285782"
),
(
"-115.114960"
),
(
"-115.108841"
),
(
"-115.280380"
),
(
"-115.120628"
),
(
"-115.166840"
),
(
"-115.224167",
"-115.172226"
)
)
2013-09-10 10:14:35.776 JoySync[597:5803] Name: (
(
"36.110806"
),
(
"36.033428"
),
(
"36.158482"
),
(
"36.029102"
),
(
"36.076591"
),
(
"36.143978"
),
(
"35.997353"
),
(
"36.106091"
),
(
"36.238758",
"36.067402"
)
)
Now I'm trying to create a for loop that takes each of these values and
stores them as a doublevalue in order to send it through a method. Here's
that for loop:
for(NSDictionary *location in _result) {
CLLocationCoordinate2D annotationCoordinate =
CLLocationCoordinate2DMake([location
valueForKey:@"merchants.branches.latitude"]
doubleValue]]),
([location
valueForKey:@"merchants.branches.longitude"]
doubleValue]]);
Annotation *annotation = [[Annotation alloc] init];
annotation.coordinate = annotationCoordinate;
annotation.title = location[@"name"];
annotation.subtitle = nil;
[self.mapView addAnnotation:annotation];
I'm not sure what's going on, but it keeps sending me back an error. Any
ideas? I know it has to do with the keys I'm trying to call.

No comments:

Post a Comment