Converting a string to double for coordinate use, Xcode

coordinates, double, objective-c, string

Solution

Solved the issue. After counting the length of the strings like Phillip said to do it turned out the length was 1 character longer than the string. So i added another string in-between that was the same string as 'latstring' and 'lonstring' however it began at index 1 rather than 0, therefore cutting off whatever character must have been infront of the coordinate value. This then converted to double perfectly.

Here is the code is used:

NSString *latstring = theList.lat;
NSString *lonstring = theList.lon;
NSLog(@"%@, %@ wooo", latstring, lonstring);

NSString *latcutstring = [latstring substringFromIndex:1];
NSLog(@"cut lat: %@", latcutstring);
NSString *loncutstring = [lonstring substringFromIndex:1];
NSLog(@"cut lon: %@", loncutstring);

double latdouble = [latcutstring doubleValue];
NSLog(@"latdouble: %f", latdouble);
double londouble = [loncutstring doubleValue];
NSLog(@"londouble: %f", londouble);

Thankyou Phillip Mills and Tobol for you're help.

EDIT: Nov 2012

It was white space causing problems and i have found a safer way of doing this as to not cut out needed characters, as craig and phillip said.

NSString *trimlat = [theList.lat stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *trimlon = [theList.lon stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    //Convert to double
    double latdouble = [trimlat doubleValue];
    double londouble = [trimlon doubleValue];

    //Create coordinate
    CLLocationCoordinate2D coord = {(latdouble),(londouble)};

Problem

I have a list of longitudes and latitudes in an XML file. I can print the lat and lon as a string but when i convert the string to a double i get 0. Here is my code: ``` NSString *latstring = [[NSString alloc] initWithString:theList.lat] ; NSString *lonstring = [[NSString alloc] initWithString:theList.lon]; NSLog(@"latstring: %@, lonstring: %@", latstring, lonstring); double latdouble = [latstring doubleValue]; double londouble = [lonstring doubleValue]; NSLog(@"latdouble: %g, londouble: %g", latdouble, londouble); ``` When i log 'latstring' and 'lonstring' i get the correct coordinates however when i log 'latdouble' and 'londouble' i get 0. I need the lat and lon values as double so i can use them in a mapview as it will not allow me to use a string for the coordinates. There is probably a very simple explanation for this however i am fairly new to objective-c and cant seem to find a solution for this. Any help is much appreciated.

Original source