Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

ios - Returning object from callback in Swift

I want to wrap the geocoder.geocdeAddressString in another method with other logic.

public func placemarkForSearchResult<T>(searchResult: T) -> CLPlacemark? {
        if let searchResult = searchResult as? String {
            let geocoder = CLGeocoder()
            geocoder.geocodeAddressString(searchResult, completionHandler: {
                (placemarks, error) -> Void in

                // Check for returned placemarks
                if let placemarks = placemarks where placemarks.count > 0 {
                    return placemarks[0] as! CLPlacemark // CLPlacemark is not convertible to void error message
                }
                return nil // Typd Void does not conform to protocol NilLiteralConvertible
            })
        }
    }

I have some other logic in this method that's not really relevant, but I was wondering how I can handle a situation like this where I want to return a CLPlacemark, but cannot because the completionHandler for the geocoder returns Void. I cannot change the Void parameter of the geocoder callback. Is that possible? Or am I stuck with calling a delegate method that uses the found CLPlacemark from the geocoder? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can never return a placemark from your placemarkForSearchResult function, because obtaining a placemark requires the use of an asynchronous function (geocodeAddressString). By that time that function has finished and calls back into your completion handler, your placemarkForSearchResult has already finished and returned long ago!

You need a strategy compatible with the asynchronous nature of the function you are calling. Instead of returning a placemark from placemarkForSearchResult, you need placemarkForSearchResult to accept a callback function parameter. When you have your placemark in the completion handler, you call that callback function. If that callback function is (cleverly) designed to accept a placemark parameter, you are now handing that placemark to whoever called you in the first place.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...