Swift:转义闭包捕获非转义参数“onCompletion" [英] Swift: Escaping closure captures non-escaping parameter 'onCompletion'

查看:42
本文介绍了Swift:转义闭包捕获非转义参数“onCompletion"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 swift 有问题.我正在尝试发送 API 请求,然后检索数据,但收到以下错误消息:

<块引用>

Swift:转义闭包捕获非转义参数 'onCompletion'".D有谁知道我该如何解决这个问题?提前致谢

代码:

<预><代码>类 RestApiManager: NSObject {静态 let sharedInstance = RestApiManager()让 baseURL = "http://api.randomuser.me/"func getRandomUser(onCompletion : (JSON) -> Void) {makeHTTPGetRequest(path: baseURL, onCompletion: { json, err -> Void in完成(json)})}func makeHTTPGetRequest(path: String, onCompletion: ServiceResponse) {let request = NSMutableURLRequest(url : URL(string: path)! as URL)让会话 = URLSession.sharedlet task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in让 json:JSON = JSON(data as Any)onCompletion(json,错误为 NSError?)})任务.resume()}}

解决方案

您必须用 @escaping 标记两个完成处理程序.通常编译器会提供修复

class RestApiManager: NSObject {静态 let sharedInstance = RestApiManager()让 baseURL = "http://api.randomuser.me/"func getRandomUser(onCompletion : @escaping (JSON) -> Void) {makeHTTPGetRequest(path: baseURL, onCompletion: { json, err -> Void in完成(json)})}func makeHTTPGetRequest(path: String, onCompletion: @escaping ServiceResponse) {let request = NSMutableURLRequest(url : URL(string: path)! as URL)让会话 = URLSession.sharedlet task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in让 json:JSON = JSON(data as Any)onCompletion(json,错误为 NSError?)})任务.resume()}}

I have a problem with my swift. I am trying to send an API request and then retrieve data but I get the following error message:

"Swift: Escaping closure captures non-escaping parameter 'onCompletion'". D oes anyone know how I can solve this? thanks in advance

Code:


class RestApiManager: NSObject {
    static let sharedInstance = RestApiManager()

    let baseURL = "http://api.randomuser.me/"

    func getRandomUser(onCompletion : (JSON) -> Void) {
        makeHTTPGetRequest(path: baseURL, onCompletion: { json, err -> Void in
            onCompletion(json)
        })
    }

    func makeHTTPGetRequest(path: String, onCompletion: ServiceResponse) {
        let request = NSMutableURLRequest(url : URL(string: path)! as URL)

        let session = URLSession.shared

        let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
            let json:JSON = JSON(data as Any)
            onCompletion(json, error as NSError?)
        })
        task.resume()

    }
}

解决方案

You have to mark both completion handlers with @escaping. Usually the compiler offers a fix

class RestApiManager: NSObject {
    static let sharedInstance = RestApiManager()

    let baseURL = "http://api.randomuser.me/"

    func getRandomUser(onCompletion : @escaping (JSON) -> Void) {
        makeHTTPGetRequest(path: baseURL, onCompletion: { json, err -> Void in
            onCompletion(json)
        })
    }

    func makeHTTPGetRequest(path: String, onCompletion: @escaping ServiceResponse) {
        let request = NSMutableURLRequest(url : URL(string: path)! as URL)

        let session = URLSession.shared

        let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
            let json:JSON = JSON(data as Any)
            onCompletion(json, error as NSError?)
        })
        task.resume()

    }
}

这篇关于Swift:转义闭包捕获非转义参数“onCompletion"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆