如何将变量值传递到 URLSession 异步之外 - swift 3

rez*_*eza 0 asynchronous swift

我有这个代码:

let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
    if error != nil {
        print(error!)
        return    
    }    
    do {        
        let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

        if let parseJSON = json {        
            let getDetail = parseJSON["detail"] as? String

            returnDetail = getDetail!.base64Decoded()        
        } // parse json end    
    } // do end                
    catch {         
        print(error)    
    }    
} // let task end
Run Code Online (Sandbox Code Playgroud)

returnDetail之前已定义。我做了任何事情来设置returnDetail价值getDetail!.base64Decoded(),但它只在内部起作用let task = ...

我怎样才能将它传递到外部范围?

Dáv*_*tor 5

您可以使用多种方法来解决从异步函数内部返回值的问题。其中之一是将异步网络调用包装在函数内并使其返回completionHandler。

一些一般建议:不要使用强制展开,除非您 100% 确定您的可选值不会是nil. 对于网络请求,data即使没有错误,也可能为零,因此永远不要强制 unwrap ,请使用或data进行安全解包。解析 JSON 值时不要在 Swift 中使用,因为它没有任何效果。解析的 JSON 对象的可变性是通过使用or关键字声明保存它的变量来决定的。也不要使用,使用其本机 Swift 对应项, Dictionary (是类型的简写)。if letguard let.mutableContainersletvarNSDictionary[String:Any]Dictionary<String,Any>

func getDetail(withRequest request: URLRequest, withCompletion completion: @escaping (String?, Error?) -> Void) {
    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
        if error != nil {
            completion(nil, error)
            return    
        }    
        else if let data = data {
            do {        
                guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any] else {completion(nil, nil);return}      
                guard let details = json["detail"] as? String else {completion(nil, nil);return}
                completion(details, nil)        
            }                  
            catch {         
                completion(nil, error)  
            }
        }   
    }
    task.resume()
}
Run Code Online (Sandbox Code Playgroud)

然后你可以通过以下方式调用这个函数

getDetail(withRequest: request, withCompletion: { detail, error in
    if error != nil {
        //handle error
    } else if detail = detail {
        //You can use detail here
    }
})
Run Code Online (Sandbox Code Playgroud)