如何从locationManager()
中的callTestArray()
调用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 - Swift在另一个函数中使用变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49485691/