gpt4 book ai didi

swift - 如何在 Swift 中获取从 NSURLSessionDataTask 返回的数据

转载 作者:搜寻专家 更新时间:2023-11-01 06:24:11 24 4
gpt4 key购买 nike

我在这里提出和回答了同样的问题:How to get data to return from NSURLSessionDataTask

不同之处:我如何在 Swift 中执行此操作?我根本不知道 Objective C,所以试图解释这个答案对我来说有点徒劳..

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

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 的东西..

感谢任何帮助..

最佳答案

在 Abizern 的帮助和建议下,我终于设法编写了这个 block 或闭包,如果你愿意的话。

那么最终对我有用的是:

我修改的GetHomeData函数如下:

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()
}

然后我这样调用函数:

/*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
}

关于swift - 如何在 Swift 中获取从 NSURLSessionDataTask 返回的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com