-2

When calling my function to fetch some locations from an API I'm getting the error "Type of expression is ambiguous without more context".

As I'm relatively new to iOS development and swift I have to ask you guys. Hope its just some simple thing I've overseen. I already searched the web, but nothing helped.

Thank you in advance!

Here is how I'm calling the function:

fetchLocation(latitude: latitude, longitude: longitude, locationsCompletionHandler: {
            shopSites in
            
            ForEach (shopSites){  }
            
        })

And here is the function:

func fetchLocation(latitude: Double, longitude: Double, locationsCompletionHandler: @escaping ([ShopSite]) -> Void) {

    let semaphore = DispatchSemaphore (value: 0)
    
    let domain = "http://e2cadda8d178.ngrok.io/api/v1/"
    let urlString = domain + "public/location/" + String(latitude) + "/" + String(longitude) + "/50"
    
    //let url = URL(string: "e2cadda8d178.ngrok.io/api/v1/public/location/66.68994/10.249066/50")!
    let url = URL(string: urlString)!
    
    var request = URLRequest(url: url,timeoutInterval: Double.infinity)
    request.httpMethod = "GET"

    let task = URLSession.shared.dataTask(with: request) { json, response, error in
        guard let json = json else {
            print(String(describing: error))
            semaphore.signal()
            locationsCompletionHandler([])
            return
        }
        let str = String(decoding: json, as: UTF8.self)
        print(str)
        let shopSites: [ShopSite] = try! JSONDecoder().decode([ShopSite].self, from: json)
        print(shopSites.count)
        semaphore.signal()
        locationsCompletionHandler(shopSites)
    }

task.resume()
semaphore.wait()

Let me now if you need further details.

TOP_PTBEV
  • 12
  • 2
  • I suspect your issue is related to SwiftUI. You should tag your questions properly – Leo Dabus Mar 05 '21 at 15:55
  • Does this answer your question? [Type of expression is ambiguous without more context Swift](https://stackoverflow.com/questions/40894582/type-of-expression-is-ambiguous-without-more-context-swift) – pkamb Mar 05 '21 at 16:48
  • Hey, unrelated to the question you're asking it looks like the code above is kind of mixing metaphors. You're using a completion handler, but also using a semaphore to block the thread until you get results. The whole point of using a completion handler is to not need to block the thread until you get results. `URLSessionDataTask` will call the completion handler it was created with when the request complete, so your `locationsCompletionHandler` can still get called by the system even after `fetchLocation` has returned. – Christopher Thiebaut Mar 05 '21 at 18:33

2 Answers2

0

I think that you may have a problem with your for each loop code, try to change your callback code to

fetchLocation(latitude: latitude, longitude: longitude, locationsCompletionHandler: {
            shopSites in
            
            for site in shopSites {

            }
            
        })
Luca Pizzini
  • 1,805
  • 1
  • 3
  • 14
0

So, the debug information you get in Swift is based on the output from the compiler. Unfortunately, that means sometimes you get really terrible error messages like this when the compiler cannot figure out what you're trying to do.

I think your problem boils down to the fact that you're using ForEach which is a type of view from SwiftUI when you really want a simple for loop like: for site in shopSites.

You could also do shopSites.forEach { }, which would be another way of iterating over that array.

Anyways, I think using a view that looks like it is named like it should be a loop where you really want some kind of actual loop construct is your problem here.

In general, when you get an error like that there's no one answer. Take a step back, try to add more specific type annotations, or rephrase what you're trying to say and usually a better error message will shake out.