Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a String from CLLocationDegrees, e.g. in NSLog or StringWithFormat

Hello Stacked-Experts!

My question: How to generate a string from a CLLocationDegrees value?

Failed attempts:

1. NSLog(@"Value: %f", currentLocation.coordinate.latitude); //Tried with all NSLog specifiers.
2. NSNumber *tmp = [[NSNumber alloc] initWithDouble:currentLocation.coordinate.latitude];
3. NSString *tmp = [[NSString alloc] initWithFormat:@"%@", currentLocation.coordinate.latitude];

When I look in the definition for the CLLocationDegrees it clearly states that this is a double:

typedef double CLLocationDegrees;

What am I missing here? This is making me go crazy... Please help to save my mind!

Thanks in advance and best regards. //Abeansits

like image 439
ABeanSits Avatar asked Aug 25 '09 15:08

ABeanSits


2 Answers

These are correct:

NSLog(@"Value: %f", currentLocation.coordinate.latitude); //Tried with all NSLog specifiers.
NSNumber *tmp = [[NSNumber alloc] initWithDouble:currentLocation.coordinate.latitude];

This is wrong, because coordinate.latitude isn't an object as nsstring might expect.

NSString *tmp = [[NSString alloc] initWithFormat:@"%@", currentLocation.coordinate.latitude];

If you want an NSString:

myString = [[NSNumber numberWithDouble:currentLocation.coordinate.latitude] stringValue];

or

NSString *tmp = [[NSString alloc] initWithFormat:@"%f", currentLocation.coordinate.latitude];

Marco

like image 167
Marco Avatar answered Sep 19 '22 00:09

Marco


Swift version:

Latitude to String:

var latitudeText = "\(currentLocation.coordinate.latitude)"

or

let latitudeText = String(format: "%f", currentLocation.coordinate.latitude)
like image 35
KlimczakM Avatar answered Sep 19 '22 00:09

KlimczakM