簡體   English   中英

快速訪問類文件時上下文關閉類型錯誤

[英]Contextual closure type error when accessing the class file in swift

我是快速編程的新手,當我調用類文件時,我已經使用Microsoft Azure實施了語音到文本的操作,出現類似“ 上下文關閉類型'(Data ?, URLResponse ?, Error?)-> Void'的錯誤,期望3參數,但在閉包主體中使用了1。 “誰能幫助我解決此錯誤。

    //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()
    }

}

在此處輸入圖片說明

正如Leo Dabus所評論的那樣,您不能將單個參數閉包(您的閉包接受一個Callback類型的參數c )作為需要三參數閉包的參數。

這是SE-0110區分單元組和多參數函數類型的效果 該提案的狀態目前顯示為Deferred ,但是該提案的大多數功能已經在Swift 4中實現並生效,只有一小部分(包括解決Swift 4中的SE-0110可用性回歸 )和正在重新進行中。設計。

一種可能的解決辦法是這樣的:

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()
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM