
import UIKit
protocol HttpProtocol
{
func didReceiveResults(results:NSArray)
}
class HttpController:NSObject{
var delegate:HttpProtocol?
func onSearch(url:String){ var nsUrl:NSURL = NSURL(string:url)! var request:NSURLRequest = NSURLRequest(URL:nsUrl) NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{ (response:NSURLResponse!,data:NSData!,error:NSError!) -> Void in var httpRespOnse= response as! NSHTTPURLResponse if ( httpResponse.statusCode == 200){ var array:NSArray = NSJSONSerialization.JSONObjectWithData (data, options : NSJSONReadingOptions.AllowFragments, error:nil) as! NSArray self.delegate?.didReceiveResults(array) } }) } }
报错为:
1.Cannot convert value of type '(NSURLResponse!, NSData!, NSError!) -> Void' to expected argument type '(NSURLResponse?, NSData?, NSError?) -> Void'
2.Extra argument 'error' in call
折腾不下了,这个闭包不知道问题出在哪里。。。。
1 finab 2016-03-12 15:56:11 +08:00 via iPhone 它需要一个可选值类型的参数,你给传了!,参数类型不匹配。你把你闭包的参数类型删掉就写个参数名就可以了 |
2 iAugus 2016-03-12 16:02:15 +08:00 简单的从语法上帮你改了下,另外 sendAsynchronousRequest(...) was deprecated in iOS 9.0 ``` import UIKit protocol HttpProtocol { func didReceiveResults(results:NSArray) } class HttpController:NSObject{ var delegate:HttpProtocol? func onSearch(url: String){ let nsUrl = NSURL(string:url)! let request = NSURLRequest(URL:nsUrl) NSURLConnection.sendAsynchronousRequest(request, queue: .mainQueue()) { (response, data, error) -> Void in guard let data = data, httpRespOnse= response as? NSHTTPURLResponse else { return } if httpResponse.statusCode == 200 { do { let array = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as! NSArray self.delegate?.didReceiveResults(array) } catch { } } } } } |
3 iAugus 2016-03-12 16:03:08 +08:00 ``` import UIKit protocol HttpProtocol { func didReceiveResults(results:NSArray) } class HttpController:NSObject{ var delegate:HttpProtocol? func onSearch(url: String){ let nsUrl = NSURL(string:url)! let request = NSURLRequest(URL:nsUrl) NSURLConnection.sendAsynchronousRequest(request, queue: .mainQueue()) { (response, data, error) -> Void in guard let data = data, httpRespOnse= response as? NSHTTPURLResponse else { return } if httpResponse.statusCode == 200 { do { let array =try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as! NSArray self.delegate?.didReceiveResults(array) } catch { } } } } } |
4 iAugus 2016-03-12 16:04:46 +08:00 Sorry 代码片段怎么插入来着? 无法格式化?你将就着看吧 |
5 iAugus 2016-03-12 16:09:14 +08:00 ``` import UIKit protocol HttpProtocol { func didReceiveResults(results:NSArray) } class HttpController:NSObject{ var delegate:HttpProtocol? func onSearch(url: String){ let nsUrl = NSURL(string:url)! let request = NSURLRequest(URL:nsUrl) NSURLConnection.sendAsynchronousRequest(request, queue: .mainQueue()) { (response, data, error) -> Void in guard let data = data, httpRespOnse= response as? NSHTTPURLResponse else { return } if httpResponse.statusCode == 200 { do { let array = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as! NSArray self.delegate?.didReceiveResults(array) } catch { } } } } } ``` |
6 iAugus 2016-03-12 16:10:58 +08:00 好吧,我放弃了,不知道怎么插入代码片段。。。 |
9 ICo OP |