我有同样的问题,在这里被问和回答:如何获取数据以从 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 的方法。
任何帮助表示赞赏..