ios - Alamofire 4 从类型 '(_) throws -> ()' 的抛出函数到非抛出函数类型 '(DataResponse<Any>) -> Void' 的无效转换

标签 ios swift alamofire

我刚刚运行了 pod update 命令。并在我的 Alamofire 请求的 .responseJSON { response in block 中收到此错误。

Invalid conversion from throwing function of type '(_) throws -> ()' to non-throwing function type '(DataResponse) -> Void'

这里是截图

error screenshot

更新 1

这是我的代码

Alamofire.request(getPublicKeyUrl!, method: .get, parameters: nil, encoding: JSONEncoding.default)
                            .downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
                                print("Progress: \(progress.fractionCompleted)")
                            }
                            .validate { request, response, data in
                                // Custom evaluation closure now includes data (allows you to parse data to dig out error messages if necessary)
                                //print("response", response);
                                return .success
                            }
                            .responseJSON { response in
                                //debugPrint(response)
                                //print("JSON req", response)

                                if((response.result.value) != nil) {

                                    let swiftyJSON = JSON(response.result.value!)

                                    print(swiftyJSON)

                                    let Code = swiftyJSON["LL"]["Code"].stringValue
                                    print("response Code", Code);
                                    if (Code == "200") {

                                        // get key
                                        self._publicKey = swiftyJSON["LL"]["value"].stringValue
                                        print("publicKey", self._publicKey);

                                        var request = URLRequest(url: (URL(string:"ws://\(self._ip)/ws/rfc6455"))!)
                                        print("Request", String(describing: request.url))
                                        request.setValue("websocket", forHTTPHeaderField: "Upgrade")
                                        request.setValue("Upgrade", forHTTPHeaderField: "Connection")
                                        request.setValue("remotecontrol", forHTTPHeaderField: "Sec-WebSocket-Protocol")
                                        request.setValue(self._hash as String, forHTTPHeaderField: "Sec-WebSocket-Key")
                                        self.socket = WebSocket(request: request)

//                                        self.commandInPending.insert("authenticate/\(self._hash)", at: 0)
//                                        self.socket.delegate = self
//                                        self.socket.connect()

                                        // check AES encryption
                                        //let message     = "Don´t try to read this text. Top Secret Stuff"
                                        //let messageData = message.data(using:String.Encoding.utf8)!
                                        let keyData     = "12345678901234567890123456789012".data(using:String.Encoding.utf8)!
                                        let ivData      = "abcdefghijklmnop".data(using:String.Encoding.utf8)!

                                        print("keyData", keyData);
                                        print("ivData", ivData);

                                        let key = keyData.map{ String(format:"%02x", $0) }.joined()
                                        let iv = ivData.map{ String(format:"%02x", $0) }.joined()

                                        print("keyHex", key);
                                        print("ivData", iv);

                                        // let session_key =
                                        let keyAndiv = ("\(key):\(iv)") // self._publicKey
                                        print("keyAndiv", keyAndiv);

//                                        let str = "Clear Text"
                                        let clear = try ClearMessage(string: keyAndiv, using: .utf8)
                                        let encrypted = try clear.encrypted(with: publicKey, padding: .PKCS1)

                                        let data = encrypted.data
                                        let base64String = encrypted.base64Encoded
                                        print ("data", data);
                                        print ("base64String", base64String);


                                        //let encryptedData = AppUtils.testCrypt(data:messageData,   keyData:keyData, ivData:ivData, operation:kCCEncrypt)
                                        //let decryptedData = AppUtils.testCrypt(data:encryptedData, keyData:keyData, ivData:ivData, operation:kCCDecrypt)
                                        //let decrypted     = String(bytes:decryptedData, encoding:String.Encoding.utf8)!

                                        //print("message", message);
                                        //print("decrypted", decrypted);

                                    }
                                }
                        }

有什么线索吗?

最佳答案

发生错误是因为您没有处理throwing 函数的错误。

添加一个do - catch block

do {
     let clear = try ClearMessage(string: keyAndiv, using: .utf8)
     let encrypted = try clear.encrypted(with: publicKey, padding: .PKCS1)

     let data = encrypted.data
     let base64String = encrypted.base64Encoded
     print ("data", data);
     print ("base64String", base64String)
} catch { print(error) }

这是 Swift,你不需要在 if 条件和尾随分号周围加括号

if response.result.value != nil { ...

或更好

guard let result = response.result.value else { return }
let swiftyJSON = JSON(result)

关于ios - Alamofire 4 从类型 '(_) throws -> ()' 的抛出函数到非抛出函数类型 '(DataResponse<Any>) -> Void' 的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51616476/

相关文章:

ios - iPhone : take photo with front camera programmatically

ios - CNContact 属性编码

ios - iOS 中的动态布局

swift - 往返 Swift 数字类型到/从数据

swift - GPUImage2 录制的视频上没有图像

swift - Alamofire v4 使用未声明的类型响应

alamofire - 我的图像是否使用 Alamofire 和 AlamofireImage 自动缓存

ios - Objective C - 在代码中创建的对象在切换 View Controller 后消失

ios - 如何将导航栏标题更改为图像?其他方法没有奏效

ios - Alamofire - Alamofire.AFError.responseSerializationFailed - Xcode 8