Swift: Escaping closure captures non-escaping parameter 'onCompletion'

老子叫甜甜 提交于 2020-03-23 23:58:37

问题


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

    }
}

回答1:


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

    }
}



回答2:


Use this:

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

}
}



回答3:


This is happning due to your parameter onCompletion. By default it is @nonesacping you have to marke it @esacping so it can be worked in completionHandler closure.

func makeHTTPGetRequest(path: String, onCompletion: @escaping ServiceResponse)


来源:https://stackoverflow.com/questions/59784963/swift-escaping-closure-captures-non-escaping-parameter-oncompletion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!