Search code examples
iosswiftnsurlsessionios12

Fetch data in iOS 12


I have an error when I try to fetch data using Xcode 10 Beta and Swift 4.2.

In swift 4.1 was working perfect my method and now I have an error and I can't find a solution on internet to fix it.

Here is my code:

// Download photos from Google for products avatar
func fetchPhotosForProductsFromGoogle(){

    guard let requestURL = NSURL(string: URL.googleAPI + "q=\(query ?? "flagUK")" + URL.searchPhotosOnly + URL.googleSearchEngineID + URL.googleAPIKey)
        else{
            fatalError("URL can't be found.")
    }

    URLSession.shared.dataTask(with: requestURL) { (data, response, error) in

        guard error == nil else {
            print(error?.localizedDescription ?? "Error URL Session.")
            return
        }
        do {

            let googlePhotosList = try JSONDecoder().decode(GoogleSearchModel.self, from: data!)

        } catch {
            print(error.localizedDescription)
        }
        DispatchQueue.main.async {

            self.productsTableView.reloadData()
        }

        }.resume()
}

Here is a picture with the error:

enter image description here

Any idea will help me a lot ! Thanks.


Solution

  • The problem was in a JSON Model where was a structure named URL and because of that structure the URL class from Swift was override. So be careful when you convert your JSON model into Swift to check 2 times the name of the structures, enums etc.