Ambiguous reference to member 'dataTask(with:completionHandle:)'错误
在研究IOS的网络请求过程中,因为NSURLConnection已经过时,需要引用到URLSession
var url:NSURL=NSURL(string: "http://3g.163.com")!
var request:NSURLRequest=NSURLRequest(url: url as URL)
//创建会话对象
let session=URLSession.shared
let date = Date()
print("创建任务, 时间:\(date.timeIntervalSince1970)")
//初始化请求
let dataTask = session.dataTask(with: request, completionHandler: { (data,resp,err) in
})
编译时候一直报错,Ambiguous reference to member 'dataTask(with:completionHandle:)'错误。
开始我一直怀疑是completionHandle错误,不停的去查资料,后来突然发现,原来错误在with :request
通过定义去查看URLSession
open func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) -> URLSessionDataTask
/////////////////////////////
with request:URLRequest
我上面用NSURLRequest,我真的是醉了,这个错误错的离谱啊。
写在这里估计大家都明白错误原因了。花了一天的时间,希望此文对自己有所提高