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

时间:2018-03-26 07:03:59

标签: swift function variables

如何从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]
}

1 个答案:

答案 0 :(得分: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()
}