1

我有同样的问题,在这里被问和回答:如何获取数据以从 NSURLSessionDataTask 返回

区别:我如何在 Swift 中做到这一点?我根本不知道Objective C,所以试图解释这个答案对我来说有点徒劳。

因此,鉴于下面的代码,我有一个 FirstViewController,它将调用我的 HomeModel 类来使用 NSURLSession 调用获取数据。调用完成后,我想将数据返回给 FirstViewController,以便我可以在视图中进行设置。

FirstViewController 类看起来像:

import UIKit
class FirstViewController: UIViewController
{
    let homeModel = HomeModel()

    override func viewDidLoad()
    {
       super.viewDidLoad()

       // Go load the home page content
       SetHomeContent()
    }

  override func didReceiveMemoryWarning()
  {
      super.didReceiveMemoryWarning()
  }

  /*Call to go get home page content*/
  func SetHomeContent()
  {
      homeModel.GetHomeData();

      //TODO find a way to get result here... and set it to the textView
  }
}

HomeModel 类看起来像:

 import Foundation
 class HomeModel
 {

    private let siteContent:String = "http://www.imoc.co.nz/MobileApp/HomeContent"

   func GetHomeData()
   {
      var url : NSURL! = NSURL(string:siteContent)
      var request: NSURLRequest = NSURLRequest(URL:url)
      let config = NSURLSessionConfiguration.defaultSessionConfiguration()
      let session = NSURLSession(configuration: config)

      let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler:      {(data, response, error) in

        var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil

        let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: error) as? NSDictionary!

      // At this stage I want the jsonResult to be returned to the FirstViewController
    });

    // do whatever you need with the task
    task.resume()
}

我想我想要一种从原始 all 传入完成处理程序的方法,或者使用异步任务来等待结果的类似于 C# 5 的方法。

任何帮助表示赞赏..

4

3 回答 3

3

With the help and suggestion taken from Abizern I finally managed how to write up this block or closure if you want.

So what worked for me in the end:

The GetHomeData function I changed as follows:

private let siteContent:String = "http://www.imoc.co.nz/MobileApp/HomeContent"
// I changed the signiture from my original question
func GetHomeData(completionHandler: ((NSDictionary!) -> Void)?)
{
    var url : NSURL! = NSURL(string:siteContent)
    var request: NSURLRequest = NSURLRequest(URL:url)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)

    let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
        var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil

        let jsonResult: NSDictionary! =  NSJSONSerialization.JSONObjectWithData(data, options: nil, error: error) as? NSDictionary!

        // then on complete I call the completionHandler...
        completionHandler?(jsonResult?);
    });
    task.resume()
}

Then I call the function like this:

/*Call to go get home page content*/
func SetHomeContent()
{
    homeModel.GetHomeData(self.resultHandler)
}

func resultHandler(jsonResult:NSDictionary!)
{
    let siteContent = jsonResult.objectForKey("SiteContent") as NSDictionary
    let paraOne = siteContent.objectForKey("HomePageParagraphOne") as String
}
于 2014-11-07T09:48:54.180 回答
1

更改您的 GetHomeData() 方法,使其占用一个块。当您调用该方法时,将它传递给您想要对数据执行什么操作的块。在数据任务的完成块中,调用这个传入的块。

于 2014-11-07T07:20:29.487 回答
0

您可以将此框架用于 Swift 协程 - https://github.com/belozierov/SwiftCoroutine

DispatchQueue.main.startCoroutine {
    let dataFuture = URLSession.shared.dataTaskFuture(for: url)
    let data = try dataFuture.await().data
    . . . parse data ...
}
于 2020-05-27T15:12:00.253 回答