DispatchQueue.main.async无法访问转义的闭包参数?

时间:2019-01-18 12:34:30

标签: swift closures

请帮助我理解一些内容,我具有此功能:

 func getUserDetailsRequestFor(userId: String, completion: @escaping([DataObject?]?, ApiError?, NSError?) -> () ) {
    let token = KeychainWrapper.standard.string(forKey: "token") ?? ""
    let headers = ["Content-Type": "application/json",
                   "Authorization": "Bearer " + token]

    let endPoint = networkManager.buildURLforEndpoint(endpoint: ApiEndPoints.getUserByID.rawValue.replacingOccurrences(of: "{id}", with: userId))
    let request = networkManager.buildURLRequest(withEndpoint: endPoint, andHttpMethod: "Get", andHeaders: headers, andJsonBody: nil)

    networkManager.dataTaskWith(with: request) { (apiData, error) in
        completion(apiData?.data, apiData?.error, error)
    }
}

我这样称呼:

func getUserDetails() {
    NetworkRouter.shared.getUserDetailsRequestFor(userId: "3") { (apiData, apiError, error) in
       print("this line get executed first, and i have access to apiData, apiError, error")
        DispatchQueue.main.async {
           print("this line get executed second, and i  don't have access to apiData, apiError, error")
        }
    }
}

我已将打印语句用于说明我的确切问题。我不明白为什么我无法访问第二个print语句中的转义闭包参数。

0 个答案:

没有答案