Ios 如何让标签作为数字来阅读?

Ios 如何让标签作为数字来阅读?,ios,swift,Ios,Swift,我的地图注释在实际输入数字时效果很好,但是,我如何使用它 例如,latitudelabel.text被读取为纬度,而不是38.897957? 代码如下: func showEmergenyOnMap() { let emergency = MKPointAnnotation() emergency.title = "Ongoing Emergency" emergency.coordinate = CLLocationCoordinate2D(latitude: 38.

我的地图注释在实际输入数字时效果很好,但是,我如何使用它

例如,
latitudelabel.text
被读取为纬度,而不是38.897957? 代码如下:

func showEmergenyOnMap() {

    let emergency = MKPointAnnotation()
    emergency.title = "Ongoing Emergency"
    emergency.coordinate = CLLocationCoordinate2D(latitude: 38.897957, longitude: -77.036560)
    Map.addAnnotation(emergency)

}

UILabel
text
属性是一个
可选的
变量,因此它可以有一个
或一个
nil
。首先,您需要安全地
展开
,因为
CLLocationDegrees
初始值设定项使用非可选的
字符串
。您可以查看以下示例,了解如何将标签文本转换为
CLLocationCoordinate2D

var latitude: CLLocationDegrees = 0.0
var longitude: CLLocationDegrees = 0.0

if let latText = latitudelabel.text, let lat = CLLocationDegrees(latText) {
    latitude = lat
}

if let longText = longitudelabel.text, let long = CLLocationDegrees(longText) {
    longitude = long
}

let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

UILabel
text
属性是一个
可选的
变量,因此它可以有一个
或一个
nil
。首先,您需要安全地
展开
,因为
CLLocationDegrees
初始值设定项使用非可选的
字符串
。您可以查看以下示例,了解如何将标签文本转换为
CLLocationCoordinate2D

var latitude: CLLocationDegrees = 0.0
var longitude: CLLocationDegrees = 0.0

if let latText = latitudelabel.text, let lat = CLLocationDegrees(latText) {
    latitude = lat
}

if let longText = longitudelabel.text, let long = CLLocationDegrees(longText) {
    longitude = long
}

let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

将字符串转换为双精度

let lati = Double(label.text)
// do same for longi
然后初始化坐标

let coords = CLLocationCoordinate2D(latitude: lati, longitude: longi)

将字符串转换为双精度

let lati = Double(label.text)
// do same for longi
然后初始化坐标

let coords = CLLocationCoordinate2D(latitude: lati, longitude: longi)

在输入
CLLocationCoordinate2D
之前,您必须在
Double
中转换
String
值。如果您尝试在
Double
String
之间进行转换,则这是一个重复的问题。您的问题不清楚您希望以哪种方式进行转换。在输入
CLLocationCoordinate2D
之前,您必须在
Double
中输入
String
值。如果您尝试在
Double
String
之间进行转换,则这是一个重复的问题。您的问题不清楚您希望以哪种方式进行转换。效果很好,我只需在label.text和lati(在coords中)的结尾添加一个感叹号。非常感谢。工作非常完美,我只需要在label.text和lati(在coords中)的结尾添加一个感叹号。非常感谢。