【问题标题】:Generating a String from CLLocationDegrees, e.g. in NSLog or StringWithFormat从 CLLocationDegrees 生成字符串,例如在 NSLog 或 StringWithFormat 中
【发布时间】:2010-11-22 16:12:29
【问题描述】:

您好 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

【问题讨论】:

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


    【解决方案1】:

    这些是正确的:

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

    这是错误的,因为坐标.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];
    

    马可

    【讨论】:

    • 谢谢马可!这是真的。我的错误实际上存在于错误的内存分配中。 =( 很抱歉。
    【解决方案2】:

    Swift 版本:

    字符串的纬度:

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

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

    【讨论】:

      【解决方案3】:

      Obj-C 格式

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

      Swift 格式

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

      【讨论】:

        猜你喜欢
        • 2010-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        • 2011-05-11
        • 2016-02-22
        • 1970-01-01
        相关资源
        最近更新 更多