在swift中

时间:2018-05-26 07:30:20

标签: ios iphone swift callback swift4

我是swift编程的新手,我使用Microsoft Azure实现语音到文本,当我调用类文件时,我收到的错误如" 上下文关闭类型'(数据?, URLResponse?,错误?) - >虚空'期望3个参数,但在封闭体中使用了1个" .can任何人都可以帮我解决这个错误。

    //This is the sample code where i am calling the function in class file
        TTSHttpRequest.submit(withUrl: TTSSynthesizer.ttsServiceUri,
                                          andHeaders: [
                                            "Content-Type": "application/ssml+xml",
                                            "X-Microsoft-OutputFormat": outputFormat.rawValue,
                                            "Authorization": "Bearer " + accessToken,
                                            "X-Search-AppId": appId,
                                            "X-Search-ClientID": clientId,
                                            "User-Agent": "TTSiOS",
                                            "Accept": "*/*",
                                            "content-length": "\(message.lengthOfBytes(using: encoding))"
                        ],
                                          andBody: message.data(using: encoding)) { (c: TTSHttpRequest.Callback)  in
                                            guard let data = c.data else { return }
                                            callback(data)
                    }
//This is the class file where i am getting the error
class TTSHttpRequest {

   typealias Callback = (data: Data?, response: URLResponse?, error: Error?)

    static func submit(withUrl url: String, andHeaders headers: [String: String]? = nil, andBody body: Data? = nil, _ callback: @escaping (Callback) -> ()) {
        guard let url = URL(string: url) else { return }
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        headers?.forEach({ (header: (key: String, value: String)) in
            request.setValue(header.value, forHTTPHeaderField: header.key)
        })
        if let body = body {
            request.httpBody = body
        }
        let task = URLSession.shared.dataTask(with: request) { (c:Callback) in   //In this line i am getting above mentioned error.
            callback(c)

        }
        task.resume()
    }

}

enter image description here

1 个答案:

答案 0 :(得分:2)

正如Leo Dabus评论的那样,你不能传递一个参数闭包(你的闭包取一个c类型的Callback}作为一个期望三参数闭包的参数。

这是SE-0110 Distinguish between single-tuple and multiple-argument function types的效果。 提案的状态目前显示为延期,但此提案的大部分功能已在Swift 4中实施并生效,只有一小部分(包括Addressing the SE-0110 usability regression in Swift 4)被重新启用重新设计。

一种可能的解决方法是:

class TTSHttpRequest {

    typealias Callback = (data: Data?, response: URLResponse?, error: Error?)

    static func submit(withUrl url: String, andHeaders headers: [String: String]? = nil, andBody body: Data? = nil, _ callback: @escaping (Callback) -> ()) {
        guard let url = URL(string: url) else { return }
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        headers?.forEach({ (header: (key: String, value: String)) in
            request.setValue(header.value, forHTTPHeaderField: header.key)
        })
        if let body = body {
            request.httpBody = body
        }
        let task = URLSession.shared.dataTask(with: request) { data, response, error in //<- three arguments
            callback((data, response, error)) //<- Call the callback with one tuple.

        }
        task.resume()
    }

}