从CLLocationDegrees生成字符串,例如在NSLog或StringWithFormat中

时间:2009-08-25 15:24:03

标签: iphone objective-c cocoa-touch nsstring nslog

Hello Stacked-Experts!

我的问题:如何从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

3 个答案:

答案 0 :(得分:34)

这些都是正确的:

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

答案 1 :(得分:2)

Swift版本:

纬度到字符串:

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

let latitudeText = String(format: "%f", currentLocation.coordinate.latitude)

答案 2 :(得分:0)

Obj-C格式

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

快速格式

String(format: "%f", coordinate.latitude)