如何在swift中显示用户的高度?

如何在swift中显示用户的高度?,swift,swift3,core-location,Swift,Swift3,Core Location,我正在尝试构建一个显示用户海拔高度的应用程序,但是xCode说CLLocation没有成员“海拔高度”。我使用的代码是 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { self.Altitude.text = String(locations.altitude)} Xcode没有说“CLLocation”没有成员“altitude”,但是“[CLLo

我正在尝试构建一个显示用户海拔高度的应用程序,但是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)
}
因为这是一个面向用户的字符串,所以应该使用


一个小的样式说明:变量名(如
self.Altitude
应该小写,只有类型是PascalCase)。

位置是一个位置数组,数组没有属性
Altitude
。必须首先从数组中获取一个位置元素,如
Locations[0]。Altitude
为什么。最后"? 我怎么知道呢?因为这是最近的地点,而且(通常)是你想一起工作的地方。我明白了。无论如何,您也可以使用经理的位置(这就是我最后所做的,因为我喜欢知道我使用的代码是如何工作的)好的,这都在文档中,现在您也知道上面的代码是如何工作的;)对于特定设置,使用
location
属性也可以提供旧数据:
检查存储在此属性中的位置的时间戳总是一个好主意。如果接收器当前正在收集位置数据,但最小距离过滤器较大,则返回的位置可能相对较旧。如果是,您可以停止接收器并再次启动它以强制更新。
from