Utku Dalmaz
Utku Dalmaz

Reputation: 10172

Return Alamofire response from a function

func getStoriesNF () -> [String:Any] {

        let parameters: Parameters = ["user_id": userID]

        Alamofire.request("https://example.com/stories.php", method: .post, parameters: parameters).validate().responseJSON { response in
            switch response.result {
            case .success:
                if let json = response.result.value {

                    if let data = try JSONSerialization.jsonObject(with: json as Data, options: .allowFragments) as? [String:Any] {

                        return data

                    }
                }
            }
        }
    }

enter image description here

I am having Invalid conversion from throwing function of type '(_) throws -> _' to non-throwing function type '(DataResponse<Any>) -> Void' error.

How can I return data from this function?

Upvotes: 0

Views: 533

Answers (3)

mag_zbc
mag_zbc

Reputation: 6992

The main problem is that Alamofire.request is an asynchronous call, so you will need to use a completion handler, like so

func getStoriesNF (completion: @escaping ([String : Any]?, Error?) {

    let parameters: Parameters = ["user_id": userID]

    Alamofire.request("https://example.com/stories.php", method: .post, parameters: parameters).validate().responseJSON { response in
        switch response.result {
        case .success:
            if let json = response.result.value {
                do {
                    if let data = try JSONSerialization.jsonObject(with: json as Data, options: .allowFragments) as? [String:Any] {
                        completion(data, nil)
                } catch (let error) {
                    completion(nil, error)
                }
            }
        }
    }
}

JSONSerialization.jsonObject needs to be contained within a try-catch block since it's a throwing call

Upvotes: 0

Manoj
Manoj

Reputation: 1069

Can you please try the below code and see if this works?

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
        .responseJSON { response in
            switch response.result {
        case .success:
            if let json = response.result.value {

                if let data = try JSONSerialization.jsonObject(with: json as Data, options: .allowFragments) as? [String:Any] {

                    completion(data, nil)
                }
            }
        }
    }

Upvotes: 0

s.zainulabideen
s.zainulabideen

Reputation: 2057

You can't return data from asynchronous call like this. You may need to handle this via completion handlers.

     typealias CompletionHandler = ([String:Any]? , Error?) -> ()
    func getStoriesNF (completion: @escaping CompletionHandler)  {

        let parameters: Parameters = ["user_id": userID]

        Alamofire.request("https://example.com/stories.php", method: .post, parameters: parameters).validate().responseJSON { response in
            switch response.result {
            case .success:
                if let json = response.result.value {

                    if let data = try JSONSerialization.jsonObject(with: json as Data, options: .allowFragments) as? [String:Any] {

                        completion(data, nil)
                    }
                }
            }
        }
    }

Upvotes: 1

Related Questions