有一个没有特定数量的闭包的数组,如何嵌套它们? [英] There is an array with unspecific number of closures, how to nest them?

查看:47
本文介绍了有一个没有特定数量的闭包的数组,如何嵌套它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是简化的情况:

我要做的是手动嵌套它们。有一个优雅的实现吗?

What I do is nesting them manually. Is there an elegant implementation?

class Handler {
    func process(_ result: String, done: @escaping (String) -> Void) {
        DispatchQueue.global().async {  // do sth
            done("do something async")
        }
    }
}

func intercept(result: String, with handlers: [Handler], callback: @escaping (String) -> Void) {
    switch handlers.count {
    case 1:
        handlers[0].process(result) { r1 in
            callback(r1)
        }
    case 2:
        handlers[0].process(result) { r1 in
            handlers[1].process(r1) { r2 in
                callback(r2)
            }
        }
    // ... maybe up to 5
    default:
        callback(result)
    }
}


推荐答案

使用递归调用怎么办:

func intercept2(result: String, with handlers: [Handler], callback: @escaping (String) -> Void) {

    if handlers.isEmpty {
        callback(result)
    } else {
        handlers[0].process(result) { (r) in
            intercept2(result: r, with: Array(handlers.dropFirst()), callback: callback)
        }
    }
}

因为您要用其余的<$ c $调用 intercept() c>处理程序,第一个除外。

Because what you want is to call intercept() with the rest of the handlers except the first one.

带有示例测试:

class Handler {
    func process(_ result: String, done: @escaping (String) -> Void) {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
            let retVal = result + "-"
            print("process retVal: \(retVal)")
            done(retVal)
        }
    }
}

func intercept(result: String, with handlers: [Handler], callback: @escaping (String) -> Void) {
    switch handlers.count {
    case 1:
        handlers[0].process(result) { r1 in
            callback(r1)
        }
    case 2:
        handlers[0].process(result) { r1 in
            handlers[1].process(r1) { r2 in
                callback(r2)
            }
        }
    case 3:
        handlers[0].process(result) { r1 in
            handlers[1].process(r1) { r2 in
                handlers[2].process(r2) { (r3) in
                    callback(r3)
                }
            }
        }
    // ... maybe up to 5
    default:
        callback(result)
    }
}


func intercept2(result: String, with handlers: [Handler], callback: @escaping (String) -> Void) {

    if handlers.isEmpty {
        callback(result)
    } else {
        handlers[0].process(result) { (r) in
            intercept2(result: r, with: Array(handlers.dropFirst()), callback: callback)
        }
    }
}

let handlers: [Handler] = [Handler(), Handler(), Handler()]

let initialOne = "String-"
intercept(result: initialOne, with: handlers) { (res1) in
    print("Result Interecept1 : \(res1)")
    intercept2(result: initialOne, with: handlers) { (res2) in
        print("Result Interecept2 : \(res2)")
        print("res1 \(res1 == res2 ? "=" : "!")= res2")
    }
}

我将这两个测试放到另一个测试中,以确保第一个打印用于原始打印,第二个打印用于第二个打印。

I put the two tests inside the other one, to be sure that the first prints are for the original one, and the seconds are for the recursive one.

输出:

$>process retVal: String--
$>process retVal: String---
$>process retVal: String----
$>Result Interecept1 : String----
$>process retVal: String--
$>process retVal: String---
$>process retVal: String----
$>Result Interecept2 : String----
$>res1 == res2

这篇关于有一个没有特定数量的闭包的数组,如何嵌套它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆