ios - 打印到标签的距离
问题描述
现在我正在打印控制台内两个位置之间的距离,但是如何在标签内打印呢?
现在我正在尝试使用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)"
推荐阅读
- javascript - 在 FireFox 的新窗口中打印第一次不起作用,但之后
- python - 将 numpy 数组转换为没有 zip 的列表列表
- python - Keras:了解 test_on_batch 的结果
- c++11 - 对具有适合可变参数包的签名的成员函数的编译时存在性检查
- react-native - 为什么 PanResponder 不能使用 useEffect Hook?
- delphi - 如何在delphi中保护代码片段免受自动格式化CTRL + D
- ios - 有什么方法可以解决 iOS 应用程序的问题,而无需在 AppStore 上创建新版本?
- mysql - 无法连接到数据库服务器,拒绝用户 root@% 访问
- php - 为什么轮播不会自动改变 - PHP
- java - JavaFX jpackage 应用程序出现错误 -10810