swift - 如何快速显示用户的高度?

标签 swift swift3 core-location

我正在尝试构建一个显示用户高度的应用程序,但 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/

相关文章:

ios - 将 Firebase 登录转移到今天的扩展

ios - UITabBarController 以编程方式添加 Controller 导致通知打开时出现问题

ios - 如何使用与原始数据不同的数据重新加载 TableView ?

ios - 在删除一行 UITableView 之前发出警告

iphone - 如何以编程方式为 iPhone 上的应用程序启用位置服务

ios7 - iOS 7 后台获取定位服务

swift - 访问 For in 循环 Collection View Swift

ios - 应用忽略 supportedInterfaceOrientations

ios - NotificationServiceExtension 上的 Firebase 存储配置错误

ios - 如何阻止 Xcode 忽略我的 iOS 应用程序的 CoreLocation.framework?