打印到标签的距离
printing distance to label
现在我正在打印控制台内两个位置之间的距离,但如何在标签内打印它?
现在我正在尝试使用 self.distanceLabel.text = distance 但是使用此代码我得到以下错误行:无法分配类型的值'Double' 键入 'String?'
完整代码:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lastLocation = locations.last {
let myLocation = CLLocation(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)
let myBuddysLocation = CLLocation(latitude: 59.326354, longitude: 18.072310)
let distance = myLocation.distance(from: myBuddysLocation) / 1000
print(String(format: "The distance to the Job is %.01fkm", distance))
self.distanceLabel.text = distance
}
}
您需要分配 distance
类型的格式化字符串 double
self.distanceLabel.text = String(format: "The distance to the Job is %.01fkm", distance)
如果不需要描述,也可以像这样
self.distanceLabel.text = "\(distance)"
现在我正在打印控制台内两个位置之间的距离,但如何在标签内打印它?
现在我正在尝试使用 self.distanceLabel.text = distance 但是使用此代码我得到以下错误行:无法分配类型的值'Double' 键入 'String?'
完整代码:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lastLocation = locations.last {
let myLocation = CLLocation(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)
let myBuddysLocation = CLLocation(latitude: 59.326354, longitude: 18.072310)
let distance = myLocation.distance(from: myBuddysLocation) / 1000
print(String(format: "The distance to the Job is %.01fkm", distance))
self.distanceLabel.text = distance
}
}
您需要分配 distance
类型的格式化字符串 double
self.distanceLabel.text = String(format: "The distance to the Job is %.01fkm", distance)
如果不需要描述,也可以像这样
self.distanceLabel.text = "\(distance)"