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中)的结尾添加一个感叹号。非常感谢。