gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:54:28 52 4
gpt4 key购买 nike

我刚刚运行了 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/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com