V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ICo
V2EX  ›  Swift

学习 swift 遇到的问题

  •  
  •   ICo · 2016-03-12 15:32:31 +08:00 · 4368 次点击
    这是一个创建于 3176 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条附言  ·  2016-03-13 00:07:29 +08:00
    已解决(剩余一个警告)。
    'sendAsynchronousRequest(_:queue:completionHandler:)' was deprecated in iOS 9.0: Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h

    sendAsynchronousReques 被取代了。
    9 条回复    2016-03-12 23:50:00 +08:00
    finab
        1
    finab  
       2016-03-12 15:56:11 +08:00 via iPhone
    它需要一个可选值类型的参数,你给传了!,参数类型不匹配。你把你闭包的参数类型删掉就写个参数名就可以了
    iAugus
        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 {

    }

    }
    }

    }
    }
    iAugus
        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 {

    }

    }
    }

    }
    }
    iAugus
        4
    iAugus  
       2016-03-12 16:04:46 +08:00
    Sorry 代码片段怎么插入来着? 无法格式化?你将就着看吧
    iAugus
        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 {

    }

    }
    }

    }
    }


    ```
    iAugus
        6
    iAugus  
       2016-03-12 16:10:58 +08:00
    好吧,我放弃了,不知道怎么插入代码片段。。。
    kingcos
        7
    kingcos  
       2016-03-12 16:28:49 +08:00 via iPhone
    @iAugus 评论貌似不支持 markdown 。。
    iAugus
        8
    iAugus  
       2016-03-12 16:45:23 +08:00
    @kingcos 原来如此 thanks
    ICo
        9
    ICo  
    OP
       2016-03-12 23:50:00 +08:00
    @finab
    @iAugus
    @kingcos
    感谢你们的回复,,后来看了一些 stack overflow 就模仿改了一下
    大概跟 @iAugus 的一样,,就是出现了 5 个警告⚠️。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2937 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:22 · PVG 22:22 · LAX 06:22 · JFK 09:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.