ios – Tips on how to convert from [Any] to [String: Any]

[ad_1]

I’ve the next code

 func performCallToEndpoint(_ endpoint:String, utilizing technique:String, physique: [String:String]?, completion: @escaping (_ error:Error?, _ serverError: Error?, _ response: [Any]?) -> Void) {
        
        
        let request = createURLRequest(toEndpoint: endpoint, utilizing:technique, physique: physique)
        
        URLSession.shared.dataTask(with: request) { information, response, error in
            guard error == nil else {
                print("Error: error calling POST")
                completion(nil, error, nil)
                return
            }
            guard let information = information else {
                print("Error: Didn't obtain information")
                return
            }
            guard let response = response as? HTTPURLResponse, (200 ..< 299) ~= response.statusCode else {
                print("Error: HTTP request failed")
                return
            }
            do {
                guard let jsonObject = attempt JSONSerialization.jsonObject(with: information) as? [Any] else {
                    return
                }
                completion(nil, nil, jsonObject)
            } catch {
                print("Error: Attempting to transform JSON information to string")
                return
            }
        }.resume()
    }

I name it like this

      APICaller.sharedInstance.performCallToEndpoint("/library/" + bookId, utilizing: "GET", physique: nil) { (error, serverError, response) -> () in
                
                if let response = response {
                    let worth = response as! [String: Any]?
                    
                    if worth?["id"] != nil {
                        self.removeFromLibrary = true;
                        self.libbtn.setBackgroundImage(UIImage.init(named: "icon-bookmark-lg"), for: .regular)
                    } else {
                        self.removeFromLibrary = false;
                        self.libbtn.setBackgroundImage(UIImage.init(named: "icon-bookmark-w"), for: .regular)
                    }
                }
            }

Is that this line right?

          if let response = response {
               let worth = response as! [String: Any]?
                    

I wish to convert the response object right into a Dictionary and entry the values utilizing particular keys.

[ad_2]

Leave a Reply