Qadir Hussain
Qadir Hussain

Reputation: 8856

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

I have just run pod update command. and getting this error in my Alamofire request's .responseJSON { response in block.

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

here is screenshot

error screenshot

Update 1

here is my code

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);

                                    }
                                }
                        }

Any clue?

Upvotes: 6

Views: 3615

Answers (1)

vadian
vadian

Reputation: 285079

The error occurs because you don't handle the errors of the throwing functions.

Add a 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) }

And this is Swift, you don't need parentheses around if conditions and trailing semicolons

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

or better

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

Upvotes: 10

Related Questions