闭包:属性声明
private var myFailClosure: ((NSError) -> Void)?
private var mySuccessClosure: ((NSData)->Void)?
方法中声明,挂闭包
func postWithSNS(withUrlString urlStr:String? , params: [String:String]? , failClosure: ((NSError)->Void) , successClosure: ((NSData)->Void) ){
self.myFailClosure = failClosure
self.mySuccessClosure = successClosure
downloadMission.postWithSNS(withUrlString: urlString, params: paramDict, failClosure: { (nothing) in
print("我勒个去 继续")
Assist.showAlert("Can't Quit", comfirmClosure: nil, shower: self)
闭包的实现,
}) { (data) in
print(data)
let jsonData = try! NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
还欠缺一环:
闭包的调用:
在Connection的代理方法中
调用
//下载失败
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
if self.delegate != nil {
//代理方式
self.delegate?.download(self, failWithError: error)
}else if self.myFailClosure != nil {
//闭包方式
self.myFailClosure!(error)
}
}
func connectionDidFinishLoading(connection: NSURLConnection) {
if self.delegate != nil {
//代理方式
self.delegate?.download(self, finishWithData: self.receiveData)
}else if self.myFinishClosure != nil {
//闭包方式
self.myFinishClosure!(self.receiveData)
}
}