URLSession错误Swift 3

时间:2017-02-04 19:39:47

标签: ios swift swift3 urlsession

我有以下代码:

func downloadItems() {

    let url: NSURL = NSURL(string: urlPath)!
    var session: URLSession!
    let configuration = URLSessionConfiguration.default

    session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)

    let task = session.dataTask(with: url as URL)

    task.resume()

}

这是来自一个较旧的在线项目,Swift正在给我这个错误代码:

  

' URLSession'生成'()',而不是预期的上下文结果类型' URLSession!'。

我发现其他人在Stock Overflow上询问此问题,但所有答案都适用于旧版本的Swift,并导致其他错误。

以下是我发现的其他问题:

How to set URLSession Swift 3

URLSession error

1 个答案:

答案 0 :(得分:0)

var request = URLRequest(url: URL(string: urlPath!))
request.httpMethod = "GET"
task1 = URLSession.shared.dataTask(with: request) { data, response, error 

in guard let data = data, error == nil else {                                                 

// check for fundamental networking error

   print("error=\(error)")

 return

}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {


   // check for http errors

   print("statusCode should be 200, but is \(httpStatus.statusCode)")

   print("response = \(response)")

}

let json=JSON(data: data);

self.json=json;

self.parse(json: json)

}

task1.resume()