如何在Swift中使用NSURLSessionDataTask

如何在Swift中使用NSURLSessionDataTask,第1张

如何在Swift中使用NSURLSessionDataTask

不清楚您要问是什么 ,但我注意到您在代码中有几个错误:

  1. 您应该创建

    session
    使用
    NSURLSession(configuration: config)

  2. session.dataTaskWithRequest
    返回
    NSURLSessionDataTask
    ,因此无需将其包装在内部
    NSURLSessionDataTask()
    (也称为实例化新
    NSURLSessionDataTask
    对象)。

  3. 完成处理程序是一个闭包,下面是创建该特定闭包的方法:

    {(data: NSData!, response : NSURLResponse!, error : NSError!) in// your pre

    }

这是更新的代码:

let url = NSURL(string: "https://itunes.apple.com/search?term=(searchTerm)&media=software")let request = NSURLRequest(URL: url)let config = NSURLSessionConfiguration.defaultSessionConfiguration()let session = NSURLSession(configuration: config)let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in    // notice that I can omit the types of data, response and error    // your pre});// do whatever you need with the task e.g. runtask.resume()


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/4923514.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存