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

3

10

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

Sharice answered 25/8, 2009 at 15:24 Comment(0)
P
35

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

Pallette answered 25/8, 2009 at 15:30 Comment(1)
Thank you Marco! This is so true. My error actually resided in a faulty memory allocation. =( Sorry about that.Sharice
S
3

Swift version:

Latitude to String:

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

or

let latitudeText = String(format: "%f", currentLocation.coordinate.latitude)
Slavism answered 21/7, 2016 at 21:23 Comment(0)
S
0

Obj-C format

[[NSString alloc] initWithFormat:@"%f", coordinate.latitude];

Swift format

String(format: "%f", coordinate.latitude)
Sethsethi answered 26/6, 2019 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.