递归函数,带有完成块,检索多个 MKDirections - Swift

Posted

技术标签:

【中文标题】递归函数,带有完成块,检索多个 MKDirections - Swift【英文标题】:Recursive Function, With Completion Block, That Retrieves Multiple MKDirections - Swift 【发布时间】:2019-02-02 16:48:07 【问题描述】:

我正在尝试检索 MKRoute 数组,其中包含多条路线,这些路线都从同一个地方开始,但每条路线都有不同的目的地。

问题是我能想出的唯一方法是通过递归函数,但我找不到任何关于如何使用带有完成块的递归函数的信息。由于加载路由是异步完成的,因此需要一个完成块。

如何获得以下功能但使用完成块? “添加到退货”功能?

func factorial(of num: Int) -> Int 
    if num == 1 
        return 1
     else 
        return num * factorial(of:num - 1)
    

这是我的功能代码

func getDirections(originCoordinate: CLLocationCoordinate2D, destinationCoordinates: [CLLocationCoordinate2D], completion: @escaping(_ routes:[MKRoute]?, _ error: Error?) -> Void) 

    // Origin is the same for each route, what changes is the destination
    // A singular origin coordinate with an array of destination coordinates is passed in
    // The function would in theory be recursive, returning each route from the origin to each of the destinations.

    // Leave function if no more destination coordinates are passed
    if destinationCoordinates.count == 0 return

    // Origin
    let originPlacemark = MKPlacemark(coordinate: originCoordinate)
    let originItem = MKMapItem(placemark: originPlacemark)

    // Destination is made from the first element of the passed in destinationCoordinates array.
    let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinates.first!)
    let destinationItem = MKMapItem(placemark: destinationPlacemark)

    // Direction Request setup
    let directionRequest = MKDirections.Request()
    directionRequest.source = originItem
    directionRequest.transportType = .automobile
    directionRequest.destination = destinationItem

    let directions = MKDirections(request: directionRequest)

    // Calculating directions
    // Heart of function
    directions.calculate  (response, err) in

        // Checking if a response is returned
        guard let response = response else 
            completion(nil, err)
            return
        

        // Response is returned
        let route = response.routes[0]

        let tail = Array.dropFirst(destinationCoordinates)

        // Recursive part that I'm not sure how to properly implement
        completion([route].append(getDirections(originCoordinate, tail)), nil)

    
    // If no response is retrieved, our guard let statement sends us here

【问题讨论】:

把getDirection放在else块中,然后在补全中调用递归。 【参考方案1】:

对于带有完成处理程序的函数,在递归调用中,您需要为调用提供一个闭包,然后在该闭包中调用完成处理程序。

下面是使用factorial 的方法:

func factorial(of num: Int, completion: (Int) -> ()) 
    if num == 1 
        completion(1)
     else 
        factorial(of: num - 1)  partial in
            completion(num * partial)
        
    


factorial(of: 8)  result in
    print(result)

40320

【讨论】:

谢谢!但是当我尝试在完成处理程序中附加到我的 [MKRoute] 数组时,附加语句被划掉了。你知道如何解决这个问题吗? append 改变数组。要调用append,数组必须是var

以上是关于递归函数,带有完成块,检索多个 MKDirections - Swift的主要内容,如果未能解决你的问题,请参考以下文章

多个完成块完成后执行的函数

C语言学习笔记函数

调试 C 程序将结构传递给多个函数和递归函数

C语言笔记初级篇第三章:函数与递归

C语言笔记初级篇第三章:函数与递归

IOS/Objective-C:从完成块中检索 NSArray