How to use NSURLConnection completionHandler with swift

前端 未结 4 2014
感动是毒
感动是毒 2020-12-25 14:33

Does anybody know how handlers (blocks) work in swift? I am trying to get this code running but i can\'t find any documentation of the right syntax for the completionHandler

4条回答
  •  悲哀的现实
    2020-12-25 15:26

    sendAsynchronousRequest has been deprecated in newer versions of Swift. Move to dataTaskWithRequest, luckily it is used pretty much the same way

    let request:NSURLRequest = NSURLRequest(URL:NSURL(string:"http://YOUR_DESIRED_URL.com")!)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)
    
    let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
    
    });
    
    task.resume()
    

提交回复
热议问题