CLLocationDegrees到Swift中的String变量

Bis*_*128 11 xcode geolocation ios swift

鉴于代码

var latitude = userLocation.coordinate.latitude
Run Code Online (Sandbox Code Playgroud)

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

我可以将变量打印到控制台而没有任何问题,但显然这对我没有多大帮助!

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

var latitude = userLocation.coordinate.latitude.description
Run Code Online (Sandbox Code Playgroud)

但是这让我回归?

谢谢

小智 27

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

var latitudeText:String = "\(userLocation.coordinate.latitude)"
Run Code Online (Sandbox Code Playgroud)


zis*_*oft 10

let latitudeText = String(format: "%f", userLocation.coordinate.latitude)
Run Code Online (Sandbox Code Playgroud)

根据需要设置格式参数.


小智 7

这里没什么用的.我得到的最接近的是纬度值的"可选(37.8)",我认为因为CLLocationCoordinate2D是一个可选的类参数.这是我最终做的一个简单的字符串:

let numLat = NSNumber(double: (self.myLocation?.latitude)! as Double)
let stLat:String = numLat.stringValue
Run Code Online (Sandbox Code Playgroud)


Nik*_* M. -1

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

var oneString = String(userLocation.coordinate.latitude)
Run Code Online (Sandbox Code Playgroud)