0

I have the following code, and I'm trying to save the contents of shops, but the function keeps returning an empty array. Does anyone know why?

func getAllShops(endPoint: String) -> [Shop]{
    var finalResult:[Shop] = []
    AF.request(self.url + endPoint, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil, interceptor: nil, requestModifier: nil).response {
        (responseData) in
        guard let data = responseData.data else {return}
        
        do{
        let shops = try JSONDecoder().decode([Shop].self, from: data)
            finalResult = shops
            
        } catch {
            print("decoding error: \(error)")
        }
        
    }
    print(finalResult)
    return finalResult
}
User1396
  • 23
  • 1
  • 5

0 Answers0