xcode CLLocationDegrees 到 Swift 中的字符串变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26142441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:45:20  来源:igfitidea点击:

CLLocationDegrees to String variable in Swift

iosxcodeswiftgeolocation

提问by Biscuit128

Given that the code

鉴于代码

var latitude = userLocation.coordinate.latitude

returns a CLLocationDegrees Object, how can I store the value in to a variable so that I can apply it as the text of a label.

返回一个 CLLocationDegrees 对象,如何将值存储到变量中,以便我可以将其作为标签文本应用。

I can print the variable to the console without any issues but obviously that doesn't help me too much!

我可以毫无问题地将变量打印到控制台,但显然这对我没有太大帮助!

Looking through the valuable options through the autocomplete i see there is a description;

通过自动完成查看有价值的选项,我看到有一个描述;

var latitude = userLocation.coordinate.latitude.description

But this is returning me null?

但这让我返回空?

Thanks

谢谢

采纳答案by Nikos M.

You can turn it to a String using this code:

您可以使用以下代码将其转换为字符串:

var oneString = String(userLocation.coordinate.latitude)

回答by hedzs

since CLLocationDegrees is just a typedef for Double, you can easily assign it to a string var, ie:

由于 CLLocationDegrees 只是 Double 的 typedef,您可以轻松地将其分配给字符串 var,即:

var latitudeText:String = "\(userLocation.coordinate.latitude)"

回答by zisoft

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

Set the format paramater to your needs.

根据您的需要设置格式参数。

回答by Josh Goldberg

nothing here worked for me. The closest I got was "Optional(37.8)" for a latitude value, I think because the CLLocationCoordinate2D is an optional class parameter. Here is what I ended up doing to get a plain string:

这里没有什么对我有用。我得到的最接近的是纬度值的“可选(37.8)”,我认为是因为 CLLocationCoordinate2D 是一个可选的类参数。这是我最终得到一个普通字符串的方法:

let numLat = NSNumber(double: (self.myLocation?.latitude)! as Double)
let stLat:String = numLat.stringValue

回答by Supratik Majumdar

var lat = userLocation.coordinate.latitude
var latData = String(stringInterpolationSegment: lat)