如何将参数传递给在 swift 中调用转义函数的转义函数?

How to pass parameter to a escaping function that calls escaping function in swift?

func fetchParseNSyncJson2(parseNSyncFunction: @escaping(String)->Void) {
    if 200 == 200 {
        print("Got the json")
        parseNSyncFunction("The json data")
    }
}

func parseNSync2(jsonDictionary: String) {
    print("Parsed the data: \(jsonDictionary)")
}

fetchParseNSyncJson2(parseNSyncFunction: parseNSync2(jsonDictionary:))

上面的代码工作正常,但我想向 parseNSync2 函数添加另一个参数来转义闭包块,像这样

func parseNSync2(jsonDictionary: String, done: @escaping(Bool)->Void {
    print("Parsed the data: \(jsonDictionary)")
}

那我想传递这个新函数 (with escaping block) in fetchParseNSyncJson2 function just like I did above, 我该怎么做??

这是我试过的(注意:下面的代码包含错误)

func fetchParseNSyncJson(parseNSyncFunction: (json: String, done: (Bool)->Void)) {
    if 200 == 200 {
        print("got the json")
        
        parseNSyncFunction(json: "json data", done: (_:)) // how should i pass parameter here??
    }
}

func parseNSync(_ jsonDictionary: String, done: @escaping (Bool)->Void) {
    print("Parsed: \(jsonDictionary)")
}

fetchParseNSyncJson(parseNSyncFunction: parseNSync(json:, done: { done in // how to i pass parameter here
                                                        if done {
                                                            print("Done")
                                                        }
}))

以下是您可以尝试的方法 -

import Foundation

typealias DoneBlock = ((_ success: Bool) -> Void)
typealias ParseNSyncBlock = ((_ json: String, _ done: @escaping DoneBlock) -> Void)

func fetchParseNSyncJson(parseNSyncBlock: @escaping ParseNSyncBlock) {
    if 200 == 200 {
        print("got the json")
        parseNSyncBlock("the json", { success in
            print("success : \(success)")
        })
    }
}
func parseNSync(_ json: String, _ done: @escaping DoneBlock) {
    print("Parsed: \(json)")
    done(true)
}

fetchParseNSyncJson(parseNSyncBlock: parseNSync)

输出

got the json
Parsed: the json
success : true