gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:25:36 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com