user567677
user567677

Reputation:

Trouble getting the returned objectForKey as NSString

Edit:

Sorry, it was late when I posted the question, lets see if I can make this more clear. Currently my app has no compile errors and runs, but what I'm expecting it to do is place pins on an MKMapview. When I run it, no pins are being displayed on the map. When I investigate the console there are no errors or indications shown. I've narrowed the bug down to these two lines:

NSString* lng_coord = [[dict objectForKey:key] description];
[CelebsAnnotation setLongitude:[lng_coord doubleValue]];

The problem is that I can't seem to get lng_coord doubleValue to give me a double value because if it was then the pins would appear. I know this because if I do this:

[CelebsAnnotation setLongitude:54.39281]; // for example

then the pins appear on the map. So if lng_coord isn't giving me a doubleValue, that makes me believe that it must not be an NSString, which would mean that objectForKey isn't returning an NSString (although I have every reason to believe that it is because I used):

// Use when fetching text data
NSString *response = [request responseString];

and then parsed that response string using SBJSON. I know that I am successfully parsing the lng / lat values from my JSON response because I can do this:

NSLog(@"%@", lng_coord);

and it prints out the correct number values in the console. I've tried casting the return value from objectForKey like so:

NSString* lng_coord = (NSString*)[dict objectForKey:key];

with no success. I then searched around SO for a way to turn the return value from objectForKey into an NSString and "description" seemed to be the answer, although that also hasn't worked for me.

SO.. to summarize, I need an NSString value for lng_coord so that I can get the doubleValue of that string! Thank you for your time everyone. Nothing but love for Stack Overflow.

Here is my JSON encoded response from my PHP script which is being parsed by my app:

[
 {"lng":"49.2796758","lat":"-123.1365125"},
 {"lng":"49.2695877","lat":"-123.0443507"},
 {"lng":"50.4547222","lat":"-104.6066667"},
 {"lng":"49.2696243","lat":"-123.0696036"},
 {"lng":"49.2688942","lat":"-123.1388003"},
 {"lng":"49.2796758","lat":"-123.1365125"}
]

And here is the function that contains the bug I described above:

- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *response = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *pins = [parser objectWithString:response error:nil]; // an array of dictionaries that contain coords

// loop through the pins array of dictionaries (lng, lat)
for (int i = 0; i < [pins count]; i++)
{
    NSDictionary *dict = [pins objectAtIndex:i];
    for(NSString *key in dict) {
        if ([key isEqualToString: @"lng"]) { // access of the longitude coord
            NSString* lng_coord = [[dict objectForKey:key] description];
            [CelebsAnnotation setLongitude:[lng_coord doubleValue]];    
        }
        if ([key isEqualToString:@"lat"]) { // access of the latitude coord
            NSString* lat_coord = [[dict objectForKey:key] description];
            [CelebsAnnotation setLatitude:[lat_coord doubleValue]];
        }
    }
    // now that you've set the lng/lat, insert the annotation at index i
    CelebsAnnotation *newAnnotation = [[CelebsAnnotation alloc] init]; // template object for all pins
    [self.mapAnnotations insertObject:newAnnotation atIndex:i];
    [newAnnotation release];
    [self.mapView removeAnnotations:self.mapView.annotations];
    [self.mapView addAnnotation:[self.mapAnnotations objectAtIndex:i]]; // add annotation
}

Upvotes: 1

Views: 4071

Answers (2)

lostfound
lostfound

Reputation: 41

I had the same issue.

Was consuming a third party service which returned string in json. My own service was returning that same value as long(.Net WCF). Comparison of keys in the dictionary failed.

I changed my return type to string to match the third party service type and it worked fine.

Upvotes: 0

teriiehina
teriiehina

Reputation: 4761

what is the format of the object in your dict ? If it's just a string, you don't need to use "description" in

NSString* lng_coord = [[dict objectForKey:key] description];

If you want to be sure to have a NSString, use

NSString* lng_coord = (NSString*)[dict objectForKey:key];

Upvotes: 4

Related Questions