我想解析数据使用AFNetworking & swift 3.0和xcode 8.0,但我得到像下面的错误。下面的代码适用于swift 2.3但不工作在3.0AFNetwoking与迅速3.0&xcode 8:POST请求和GET请求
或者如果有人知道AFNetworking & swift 3.0使用xcode 8.0进行POST & GET请求请告诉我。以简单的例子。 在此先感谢
您可以看到下面的错误。
func callApi(apiName: String, param: [String : AnyObject]?, data: NSDictionary?, withMethod type: String, CompletionHandler:@escaping (_ code: Int, _ error:NSError?, _ response:AnyObject?) -> Void)
{
MBProgressHUD.showAdded(to: AppDelegateObj.window, animated: true)
let str_URL : String = kHOSTPATH+apiName
let manager: AFHTTPSessionManager = AFHTTPSessionManager()
if (type == kREQ_POST) {
manager.POST(str_URL, parameters: param, constructingBodyWithBlock: { (formData: AFMultipartFormData!) in
if data?.allValues.count != 0 && data != nil
{
let fileUrl = NSURL(fileURLWithPath: (data?.valueForKey("filePath"))! as! String)
try! formData.appendPartWithFileURL(fileUrl, name: (data?.valueForKey("key"))! as! String)
}
}, progress: { (NSProgress) in
}, success: { (task:URLSessionDataTask, responseObject) -> Void in
CompletionHandler(code: 1, error: nil, response:responseObject)
MBProgressHUD.hideHUDForView(AppDelegateObj.window, animated: true)
}, failure: { (task:URLSessionDataTask?, error:NSError) -> Void in
CompletionHandler(code: 0, error: error, response:nil)
MBProgressHUD.hideHUDForView(AppDelegateObj.window, animated: true)
})
}
else {
manager.GET(str_URL, parameters: param, progress: { (NSProgress) in
}, success: { (task:URLSessionDataTask, responseObject) -> Void in
CompletionHandler(code: 1, error: nil, response:responseObject)
MBProgressHUD.hideHUDForView(AppDelegateObj.window, animated: true)
}, failure: { (task:URLSessionDataTask?, error:NSError) -> Void in
CompletionHandler(code: 0, error: error, response:nil)
MBProgressHUD.hideHUDForView(AppDelegateObj.window, animated: true)
})
}
}
,但我得到的错误是这样
cannot convert the value of type (URLSessionDataTask?,NSError)->Void to expected argument type '((URLSessionDataTask?,NSError)->Void)?'
请参考链接(http://stackoverflow.com/questions/39508868/completion-handlers-错误在迅速3和Xcode 8),这将帮助你。 –