10

你好堆叠专家!

我的问题:如何从 CLLocationDegrees 值生成字符串?

失败的尝试:

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];

当我查看 CLLocationDegrees 的定义时,它清楚地表明这是一个双精度:

typedef double CLLocationDegrees;

我在这里想念什么?这让我发疯了......请帮助拯救我的心灵!

提前致谢并致以最诚挚的问候。//Abeansits

4

3 回答 3

35

这些是正确的:

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

这是错误的,因为 coordinate.latitude 不是 nsstring 所期望的对象。

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

如果你想要一个 NSString:

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

或者

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

马可

于 2009-08-25T15:30:05.263 回答
2

斯威夫特版本:

字符串的纬度:

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

或者

let latitudeText = String(format: "%f", currentLocation.coordinate.latitude)
于 2016-07-21T21:23:09.957 回答
0

Obj-C 格式

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

斯威夫特格式

String(format: "%f", coordinate.latitude)
于 2019-06-26T18:14:36.213 回答