用什么参数代替(String)-> void?

时间:2019-05-08 12:03:09

标签: swift

我正试图从很长时间内解决此问题。

我不明白该代替“(String)-> void”传递给该函数什么,因为它应该返回一个字符串:

var result = myobj.createData(request: request, with: (String) -> void)

上面的代码正在调用以下函数:

func createData(request:Crudpb_CreateRequest, with completion: @escaping (String) -> Void) {
    DispatchQueue.main.async {
        self.response = try! self.client.create(request)
        completion(self.response.result)
    }
}

1 个答案:

答案 0 :(得分:1)

调用此函数时,您需要传递类型为(String) -> void的闭包

myobj.createData(request: request, with: { string in
    print(string)
})

var completion = { string in
    print(string)
}
myobj.createData(request: request, with: completion)

您可以像这样存储结果

var result = ""
myobj.createData(request: request, with: { string in
    result = string
    self.displayTextArea.text = result
    print(result)
})