gpt4 book ai didi

swift - 从 swift 2 转换代码时出现关闭错误

转载 作者:行者123 更新时间:2023-11-30 12:25:36 25 4
gpt4 key购买 nike

我有以下代码,其中我使用一些参数发出激活请求

        /*
Make an API request for activation
*/
var parameters: [String : AnyObject] =
[
"code" : code as AnyObject,
"dob" : httpClient.dateOfBirthFormatter.string(from: dateOfBirth) as AnyObject,
"csr" : CSR as AnyObject,
"deviceType" : systemVersion as AnyObject,
"deviceModel" : deviceInfo as AnyObject
]
if let deviceId = deviceId {
parameters["deviceId"] = deviceId as AnyObject?
}

httpClient.devicePOST("api/activation", parameters: parameters as AnyObject?, handleAuthorization: false, start: nil, retry: nil,

success:
{
(task: URLSessionDataTask, result: AnyObject?) -> Void in

guard let response = result as? [String : AnyObject] else {
failure(NSError(domain: "", code: 0, userInfo: ["reason" : "Invalid response"]))//change this later
return
}

guard let certificatePEM = response["certificate"] as? String else {
failure(NSError(domain: "", code: 0, userInfo: ["reason" : "Invalid response: certificate"]))//change this later
return
}

/*
Data is valid, activation is successful. Retrieve optional device identifier and call success handler
*/
var deviceId: UUID?
if let identifier = response["id"] as? String {
deviceId = UUID(uuidString: identifier)
}

success(certificatePEM, customerId, patientId, deviceId)
},

failure: {

(task: URLSessionDataTask?, error: NSError) -> Void in

if let data = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] as? Data {
if let string = String(data: data, encoding: String.Encoding.ascii) {
print(string)
}
}

failure(error)
}
)
}

设备POST函数定义如下

typealias StartSessionTaskHandler = (URLSessionDataTask) -> Void
typealias RetrySessionTaskHandler = (URLSessionDataTask) -> Void
typealias SuccessResponseHandler = ( URLSessionDataTask, AnyObject?) -> Void
typealias FailureResponseHandler = (URLSessionDataTask?, NSError) -> Void


func devicePOST(_ URLString: String, parameters: AnyObject?, handleAuthorization: Bool = true, start: StartSessionTaskHandler?, retry: RetrySessionTaskHandler?, success: SuccessResponseHandler?, failure: FailureResponseHandler?) {

guard let deviceService = deviceService else {

failure?(nil, NSError(domain: "", code: 0, userInfo: ["reason": "device service is unavailable"]))
return
}

POST(deviceService, URLString: URLString, parameters: parameters, handleAuthorization: handleAuthorization, start: start, retry: retry, success: success, failure: failure)
}

在 POST 函数中,当我尝试检查闭包 block 时,我看到类似这样的内容

POST function closure description

Success closure description

所以我猜这些 block 没有正确传递。当我使用 swift 2 时,这工作得很好。有人可以帮我解决这个问题吗?提前致谢

最佳答案

通过定义失败和成功的 block 来修复。

我将成功关闭定义为

  success : {
(task:URLSessionDataTask,object: Any?) -> Void in
success(task,object)
}, failure: failure)

这通常是我将数据传回堆栈的方法

关于swift - 从 swift 2 转换代码时出现关闭错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44261412/

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