我有以下代码,其中我使用某些参数发出激活请求
/*
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函数中,当我尝试检查闭包块时,我看到类似这样的内容
所以我猜这些块没有正确传递。当我使用swift 2时,这工作正常。有人可以帮我解决这个问题。提前致谢
答案 0 :(得分:0)
通过定义失败和成功的块来解决。
我将成功结束定义为
success : {
(task:URLSessionDataTask,object: Any?) -> Void in
success(task,object)
}, failure: failure)
这通常是我在堆栈中传回数据时所遵循的