How to convert an Alamofire response to string?

Viewed 102

Does someone knows how to convert the alamofire response of type () to a string ?

Here is an exemple of request :

func fetch(jan: String) { AF.request("https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemSearch?appid=XXXXXX&jan=\(jan)&hits=50").responseDecodable(of: Welcome.self)
    { response in
        print("\(response.value?.resultSet.the0.result.the1?.price.value ?? "is nil")")
        }
}

I would like that the function fetch() returns a string instead of a () object in order to display it later in a displayLabel.text for example.

Thank you in advance !

1 Answers

fetch is calling an asynchronous function to eventually have a String, so it should not itself return a String.

Instead, it should take a completion function of type (String) -> Void that you will call where you are currently printing.

When you call fetch, you will pass a closure to it that sets the label’s text. Remember to dispatch to the main queue to do this.

Related