Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 执行块后如何返回值?敏捷的_Ios_Swift_Swift2 - Fatal编程技术网

Ios 执行块后如何返回值?敏捷的

Ios 执行块后如何返回值?敏捷的,ios,swift,swift2,Ios,Swift,Swift2,我想从函数中得到一个值。函数中有一个块。当块执行时,函数已返回值。我尝试了许多不同的方法,但都没有帮助我。我使用了NSOperation和Dispatch。在执行块之前,函数始终返回值 var superPlace: MKPlacemark! func returnPlaceMarkInfo(coordinate: CLLocationCoordinate2D) -> MKPlacemark? { let location = CLLocation(latitude: c

我想从函数中得到一个值。函数中有一个块。当块执行时,函数已返回值。我尝试了许多不同的方法,但都没有帮助我。我使用了
NSOperation
和Dispatch。在执行块之前,函数始终返回值

   var superPlace: MKPlacemark!

 func returnPlaceMarkInfo(coordinate: CLLocationCoordinate2D) -> MKPlacemark? {
    let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
    geocoder.reverseGeocodeLocation(location) { (arrayPlaceMark, error) -> Void in
            if error == nil {
                let firstPlace = arrayPlaceMark!.first!
                let addressDictionaryPass = firstPlace.addressDictionary as! [String : AnyObject]

                self.superPlace = MKPlacemark(coordinate: location.coordinate, addressDictionary: addressDictionaryPass)
            }
        }

    return superPlace
}

这件事反复出现。简单的回答是“你不能。”

函数返回时,结果不可用。异步调用在后台进行

您要做的是重构returnPlacemarkInfo函数以完成闭包

我最近一直在Objective-C工作,所以我的Swift有点生锈,但它可能看起来像这样:

 func fetchPlaceMarkInfo(
  coordinate: CLLocationCoordinate2D, 
  completion: (thePlacemark: MKPlacemark?) -> () )
 {
 }
然后在调用它时,传入一个完成闭包,一旦placemark可用,就会调用该闭包

编辑: 我编写了一个演示项目,并将其发布在Github上,模拟处理异步网络下载。看看


具体看一下方法
asynchfetchimage()
,它几乎完全符合我们所说的:在内部使用异步方法,并获取完成异步加载后调用的完成块。

您不能简单地返回此处,因为reverseGeocodeLocation函数正在异步运行,因此您需要使用自己的完成块:

var superPlace: MKPlacemark!

func getPlaceMarkInfo(coordinate: CLLocationCoordinate2D, completion: (superPlace: MKPlacemark?) -> ()) {
    let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
    geocoder.reverseGeocodeLocation(location) { (arrayPlaceMark, error) -> Void in
        if error == nil {
            let firstPlace = arrayPlaceMark!.first!
            let addressDictionaryPass = firstPlace.addressDictionary as! [String : AnyObject]

            self.superPlace = MKPlacemark(coordinate: location.coordinate, addressDictionary: addressDictionaryPass)
            completion(superPlace: superPlace)
        } else {
            completion(superPlace: nil)
        }
    } 
}

如果您使用的是完成处理程序,则函数不应返回任何内容。是的,您100%正确,我忘记将其取出,谢谢。如果它不返回任何内容,则OP的函数名
returnPlaceMarkinfo
就不太合适<代码>getPlaceMarkInfo将是重构函数的更好名称。