简体   繁体   English

快速函数返回值未正确返回

[英]swift function return values not returning correctly

   func getLatsAndLongs() -> (LATITUDE: Double, LONGITUDE: Double, DESIREDLAT: Double, DESIREDLONG: Double) {



    self.forwardGeocoding("\(addressTxtFld.text) \(cityTxtFld.text), \(stateTxtFld.text)", completion: {
        success, coordinate in

        if success {
            self.lat = coordinate.latitude
            self.long = coordinate.longitude

            print("\(self.lat) is the latitude for the initial location")
            print("\(self.long) is the longitude for the initial location")

            self.INITIAL_DESTINATION_LATITUDE = self.lat
            self.INITIAL_DESTINATION_LONGITUDE = self.long

            var initialLocation = CLLocationCoordinate2DMake(self.INITIAL_DESTINATION_LATITUDE, self.INITIAL_DESTINATION_LONGITUDE)



        } else {
            print("Error at forwardGeocoding @willcohen @ERRORALERT")
        }
    })


    self.forwardGeocodingDesired("\(addressTxtFldDest.text) \(cityTxtFldDest.text), \(stateTxtFldDest.text)", completion: {
        success, coordinate in

        if success {
            self.desiredLat = coordinate.latitude
            self.desiredLong = coordinate.longitude

            print("\(self.desiredLat) is the latitude for the desired location")
            print("\(self.desiredLong) is the longitude for the desired locaiton")

            self.DESIRED_DESTIANTION_LATITUDE = self.desiredLat
            self.DESIRED_DESTINATION_LONGITUDE = self.desiredLong

            var desiredLocation = CLLocationCoordinate2DMake(self.DESIRED_DESTIANTION_LATITUDE, self.DESIRED_DESTINATION_LONGITUDE)




        } else {
            print("Error at forwardGeocodingDesired @willcohen @ERRORALERT")
        }

    })

    return (lat,long,desiredLat,desiredLong)

}


           let latsAndLongs = getLatsAndLongs()

        let latFF = latsAndLongs.LATITUDE
        let longFF = latsAndLongs.LONGITUDE
        let latDFF = latsAndLongs.DESIREDLAT
        let longDFF = latsAndLongs.DESIREDLONG

        print("\(latFF) final lat")
        print("\(longFF) final long")
        print("\(latDFF) final latD")
        print("\(longDFF) final longD")

Okay. 好的。 So, when I try to print all of the lines on the last 4 lines, it comes out as "0" each time. 因此,当我尝试在最后4行上打印所有行时,每次都会显示为“ 0”。 Note, the two geocoding lines ( self.forwardGeocoding ) & ( self.forwardGeocodingDesired ) are not the problems, they work fine, but I have no idea why they are not printing the correct Double values. 请注意,两条地理编码行( self.forwardGeocoding )和( self.forwardGeocodingDesired )并不是问题,它们可以正常工作,但是我不知道为什么它们没有打印正确的Double值。 Any suggestions would be greatly appreciated, thank you. 任何建议将不胜感激,谢谢。

forwardGeocoding and forwardGeocodingDesired invoke an operations over the network which takes time to executes. forwardGeocodingforwardGeocodingDesired在网络上调用操作,这需要时间才能执行。 This is done using a background thread so as not to block the UI. 这是使用后台线程完成的,以免阻塞UI。 When the operations complete the code in the completion closures is executed. 当操作完成时,将执行完成闭包中的代码。

Your getLatsAndLongs function returns lat , long , desiredLat and desiredLong before the operations have completed and therefore before these variables have been set by your closure. getLatsAndLongs函数返回latlongdesiredLatdesiredLong操作之前已经完成,因此前这些变量都被你封设置。

You can pass a completion handler to getLatsAndLongs to be invoked after the operations are complete, but your situation is complicated because you are executing two geocoding operations in parallel, and you don't know which will finish first. 您可以将完成处理程序getLatsAndLongs给操作完成后要调用的getLatsAndLongs ,但是您的情况很复杂,因为您正在并行执行两个地理编码操作,并且您不知道哪个先完成。 You could dispatch the second one from the completion handler of the first, but this will be slower. 您可以从第一个的完成处理程序分派第二个,但这会比较慢。 A better approach is to use a dispatch_group: 更好的方法是使用dispatch_group:

func getLatsAndLongs(completion:(initialLocation: CLLocationCoordinate2D?, desiredLocation: CLLocationCoordinate2D?)->Void)  {

    let dispatchGroup = dispatch_group_create()

    var initialLocation: CLLocationCoordinate2D?
    var desiredLocation: CLLocationCoordinate2D?

    dispatch_group_enter(dispatchGroup)

    self.forwardGeocoding("\(addressTxtFld.text) \(cityTxtFld.text), \(stateTxtFld.text)", completion: {
        success, coordinate in
        if success {
            initialLocation = coordinate
        } else {
            print("Error at forwardGeocoding @willcohen @ERRORALERT")
        }
        dispatch_group_leave(dispatchGroup)
    })


    self.forwardGeocodingDesired("\(addressTxtFldDest.text) \(cityTxtFldDest.text), \(stateTxtFldDest.text)", completion: {
        success, coordinate in

        if success {
            desiredLocation = coordinate
        } else {
            print("Error at forwardGeocodingDesired @willcohen @ERRORALERT")
        }
        dispatch_group_leave(dispatchGroup)
    })

    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue()) {
        completion(initialLocation:initialLocation,desiredLocation:desiredLocation)
    }
}

Usage: 用法:

getLatsAndLongs { (initialLocation, desiredLocation) in
    print(initialLocation)
    print(desiredLocation)
}

Check if the completion blocks in self.forwardGeocoding and self.forwardGeocodingDesired execute asynchronously. 检查self.forwardGeocoding和self.forwardGeocodingDesired中的完成块是否异步执行。 If so, the values will not be set until after the prints have run. 如果是这样,则直到打印运行后才能设置值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM