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

标签 ios swift recursion mapkit mkdirection

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

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

如何使用完成 block 来获得以下功能? “添加到返回”功能?

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
}

最佳答案

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

下面是使用 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

关于ios - 递归函数,带有完成 block ,检索多个 MKDirections - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54495210/

相关文章:

python - 将所有 .pyc.py 文件递归命名为 .py 文件

algorithm - Iterative recurrent... 迭代法

iphone - 类中两个独立的委托(delegate)方法

iOS 最有效的文本匹配方式

swift - 从应用程序中以编程方式停靠 "Turn Hiding On"

swift - 不想下载所有对象

javascript - React-native:更新Catalina后通过错误进行项目

ios - "Items of new constraints"和 "All frames in container"是什么意思?

ios - 展开时意外发现 URL 为 nil

Python:当多个子节点引用回父节点时,如何在递归循环中找到多个路径?