gpt4 book ai didi

Swift 在另一个函数中使用变量

转载 作者:行者123 更新时间:2023-11-30 11:38:22 27 4
gpt4 key购买 nike

如何从 callTestArray() 中的 locationManager() 调用 testArray?我尝试输出它,但似乎不起作用。我也尝试在类的顶部声明,但它总是nil

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
let rootRef = Database.database().reference()
let geoRef = GeoFire(firebaseRef: rootRef.child(“X”))
let query = geoRef.query(at: userLocation, withRadius: 1000)

query.observe(.keyEntered, with: { key, location in
self.testArray.insert(key, at: self.testArray.count)
})
}

func callTestArray() {
var testOuput = testArray[0]
}

最佳答案

尝试这种方法:

func locationManager(_ manager: String, didUpdateLocations locations: String, completion: @escaping () -> Void) {
let userLocation:CLLocation = locations[0] as CLLocation
let rootRef = Database.database().reference()
let geoRef = GeoFire(firebaseRef: rootRef.child(“X”))
let query = geoRef.query(at: userLocation, withRadius: 1000)
query.observe(.keyEntered, with: { key, location in
self.testArray.append(key)
completion()
})
}

用法:

locationManager(XXXXXX, didUpdateLocations: YYYYYY) {
callTestArray()
}

关于Swift 在另一个函数中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49485691/

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