我正在尝试构建一个显示用户高度的应用程序,但 xCode 说 CLLocation 没有成员“高度”。我使用的代码是
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { self.Altitude.text = String(locations.altitude)}
最佳答案
Xcode 没有说“CLLocation”没有成员“altitude”,而是说“[CLLocation] 没有成员‘altitude’”。
locations
是一个 CLLocation
对象数组,并且(因为它是一个数组)没有名为 altitude
的属性。
您需要做的是从数组中提取一个值并使用它来访问海拔高度。
你可能想做这样的事情:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let altitude = locations.last?.altitude else { return }
self.Altitude.text = String(altitude)
}
由于这是一个面向用户的字符串,您应该使用 LengthFormatter
正确格式化它.
一个小样式说明:变量名(如 self.Altitude
应该小写,只有类型是 PascalCase)。
关于swift - 如何快速显示用户的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41983124/