学习 swift 遇到的问题

2016-03-12 15:32:31 +08:00
 ICo

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

折腾不下了,这个闭包不知道问题出在哪里。。。。

4362 次点击
所在节点    Swift
9 条回复
finab
2016-03-12 15:56:11 +08:00
它需要一个可选值类型的参数,你给传了!,参数类型不匹配。你把你闭包的参数类型删掉就写个参数名就可以了
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
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
2016-03-12 16:04:46 +08:00
Sorry 代码片段怎么插入来着? 无法格式化?你将就着看吧
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
2016-03-12 16:10:58 +08:00
好吧,我放弃了,不知道怎么插入代码片段。。。
kingcos
2016-03-12 16:28:49 +08:00
@iAugus 评论貌似不支持 markdown 。。
iAugus
2016-03-12 16:45:23 +08:00
@kingcos 原来如此 thanks
ICo
2016-03-12 23:50:00 +08:00
@finab
@iAugus
@kingcos
感谢你们的回复,,后来看了一些 stack overflow 就模仿改了一下
大概跟 @iAugus 的一样,,就是出现了 5 个警告⚠️。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/263004

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX