how to pass variable value to outside of URLSession async - swift 3

前端 未结 3 814

I have this code :

let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
    if error != nil {
              


        
3条回答
  •  有刺的猬
    2020-12-07 07:20

    You have several methods to tackle the issue of returning a value from inside an asynchronous function. One of them is to wrap the asynchronous network call inside a function and make it return a completionHandler.

    Some general advice: don't use force unwrapping unless you are 100% sure that your optional value won't be nil. With network requests, the data can be nil even if there's no error, so never force unwrap data, use safe unwrapping with if let or guard let. Don't use .mutableContainers in Swift when parsing a JSON value, since it has no effect. The mutability of the parsed JSON object is decided by using the let or var keyword to declare the variable holding it. Also don't use NSDictionary, use its native Swift counterpart, Dictionary ([String:Any] is a shorthand for the type Dictionary).

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

    Then you can call this function by

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

提交回复
热议问题