Swift 3,未调用URLSession dataTaskcompleteHandler

编程入门 行业动态 更新时间:2024-10-28 19:34:44
本文介绍了Swift 3,未调用URLSession dataTaskcompleteHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个库,因此不使用UIKit,即使在我的iOS应用中,相同的代码也可以工作,但是当我在命令行中执行not时.在PlayGround中,它似乎也可以正常工作.

I am writing a library, So not using UIKit, Even in my iOS app same code works, but when i execute in command line in doesn't . In PlayGround also it seems working.

由于某种原因未触发回调,因此print语句未执行.

For some reason callback is not getting triggered, so print statements are not executing.

internal class func post(request: URLRequest, responseCallback: @escaping (Bool, AnyObject?) -> ()) { execTask(request: request, taskCallback: { (status, resp) -> Void in responseCallback(status, resp) }) } internal class func clientURLRequest(url: URL, path: String, method: RequestMethod.RawValue, params: Dictionary<String, Any>? = nil) -> URLRequest { var request = URLRequest(url: url) request.httpMethod = method do { let jsonData = try JSONSerialization.data(withJSONObject: (params! as [String : Any]), options: .prettyPrinted) request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") request.httpBody = jsonData } catch let error as NSError { print(error) } return request } private class func execTask(request: URLRequest, taskCallback: @escaping (Bool, AnyObject?) -> ()) { let session = URLSession(configuration: URLSessionConfiguration.default) print("THIS LINE IS PRINTED") let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in if let data = data { print("THIS ONE IS NOT PRINTED") let json = try? JSONSerialization.jsonObject(with: data, options: []) if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode { taskCallback(true, json as AnyObject?) } else { taskCallback(false, json as AnyObject?) } } }) task.resume() }

编辑-:我正在编写一个库,因此不使用UIKit,即使在我的iOS应用中,相同的代码也可以工作,但是当我在命令行中执行时,则不能.在PlayGround中似乎也可以正常工作.

Edits -: I am writing a library, So not using UIKit, Even in my iOS app same code works, but when i execute in command line in doesn't . In PlayGround also it seems working.

推荐答案

我从头开始制作了一个简单的App. (Xcode 8 Beta 6/Swift 3) 在控制器中,我粘贴了您的代码. (加上网址创建.) 我在调试器中看到了所有内容:

I made a simple App from scratch. (Xcode 8 beta 6 / swift 3) In controller I pasted Your code. (plus url creation..) I see all in debugger:

此人已打印

此人也被打印

我回来了

所以似乎可行.

import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let URLString = "apple" let url = URL(string: URLString) let request = URLRequest(url: url!) ViewController.execTask(request: request) { (ok, obj) in print("I AM BACK") } } private class func execTask(request: URLRequest, taskCallback: @escaping (Bool, AnyObject?) -> ()) { let session = URLSession(configuration: URLSessionConfiguration.default) print("THIS LINE IS PRINTED") let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in if let data = data { print("THIS ONE IS PRINTED, TOO") let json = try? JSONSerialization.jsonObject(with: data, options: []) if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode { taskCallback(true, json as AnyObject?) } else { taskCallback(false, json as AnyObject?) } } }) task.resume() } }

更多推荐

Swift 3,未调用URLSession dataTaskcompleteHandler

本文发布于:2023-11-26 04:35:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swift   URLSession   dataTaskcompleteHandler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!