0

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.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Florentin Lupascu
  • 754
  • 11
  • 30

4 Answers4

1

Before swift 3 there was implicit bridging between between swift types and their counter types in objc but after swift 3 it is removed and you will have to be explicit while passing such type, refer here for more details

So you can do this

with: requestURL as URL

Or Use URL instead of NSURL

0

Change the NSURL to URL and I think you should be good to go:

guard let requestURL = URL(string: "someUrl.com")
    else{
        fatalError("URL can't be found.")
}
Aaron
  • 7,055
  • 2
  • 38
  • 53
0

In the left part you send the URL object but in the right it's NSURL

func dataTask(with url: URL) -> URLSessionDataTask

see here in Docs

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

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.

Florentin Lupascu
  • 754
  • 11
  • 30