gpt4 book ai didi

swift - 如何将 var 值设置为当前的纬度或经度? nikhil 提供的更新代码

转载 作者:行者123 更新时间:2023-11-30 10:41:47 26 4
gpt4 key购买 nike

我需要在类之外访问这些坐标值,以便计算某些方程,但是当我尝试将 myLat 分配给 UserCooperatives.userLat 或 UserCooperatives.userLong 时,会发生错误。

我尝试过使用结构和类以及全局变量,但都返回了各种错误。

    public class UserCoordinates {
static let shared:UserCoordinates = UserCoordinates()
var userLat: Double?
var userLong: Double?

func setUserLat(userLat: Double, userLong: Double) {
self.userLong = userLong
self.userLat = userLat
}
private init() {

}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
guard let location = locations.last else {
return
}
userLat = Double(location.coordinate.latitude)
userLong = Double(location.coordinate.longitude)
}

}

sin(my latitude)
I want to take the sin of my latitude wherever I am how would I assign the value of userLat in the class/function to this sin calculation?

最佳答案

除非变量是静态的,否则不能使用类直接访问变量。如果您希望某些东西成为一个实例,那么您应该使用单例检查下面的代码。

public class UserCoordinates {
static let shared:UserCoordinates = UserCoordinates()
var userLat: Double?
var userLong: Double?

func setUserLat(userLat: Double, userLong: Double) {
self.userLong = userLong
self.userLat = userLat
}
private init() {

}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
guard let location = locations.last else {
return
}
userLat = Double(location.coordinate.latitude)
userLong = Double(location.coordinate.longitude)
}

}
UserCoordinates.shared.setUserLat(userLat: 10.0, userLong: 10.0)
let myLat = UserCoordinates.shared.userLat

关于swift - 如何将 var 值设置为当前的纬度或经度? nikhil 提供的更新代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624184/

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