URLSession.dataTask(with: URL) error: Type of expression is ambiguous without a type annotation

I'm a long-time developer, but pretty new to Swift. I'm trying to get information from a web service (and found code online that I adjusted to build the function below). (Note: AAA_Result -- referenced towards the end -- is another class in my project)

Trouble is, I'm getting the subject error on the call to session.dataTask. Any help/suggestions/doc pointers will be greatly appreciated!!!

        var result: Bool = false
        var cancellable: AnyCancellable?

        self.name = name
        let params = "json={\"\"}}" // removed json details
        let base_url = URL(string: "https://aaa.yyy.com?params=\(params)&format=json")! // removed URL specifics
        do {
            let task = URLSession.shared.dataTask(with: base_url) { data, response, error in
                if let error = error {
                    print("Error: \(error)")
                }
                guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode)
                else {
                    print("Error \(String(describing: response))")
                }
                do {
                    let decoder = JSONDecoder()
                    let ar = try decoder.decode(AAA_Result.self, from: response.value)
                    // removed specific details...
                    result = true
                }
                catch {
                    print(error)
                }
            }
            task.resume()
            
        }
        catch {
            print(error)
        }
        return result
    }

Sheesh...I forgot the first line of the function:

func lookupAAA(name: String) -> Bool {

Not sure if that first line made it...

func lookupAAA(name: String) -> Bool {

No need to reply if you're within an hour of your original post - you have a one-hour window to edit it :)

Anyway, I think this is because you're assigning the task to a let but you haven't provided a type, and because the completion handler returns three different things, the type it returns will be ambiguous.

If you don't need to do anything with task later on (which it doesn't look like you do in your code), you can ignore the let, i.e. just use:

URLSession.shared.dataTask(with: url) {(data, response, error) in
    ...
}.resume()

I don't know what you're doing that's causing the issue. Maybe because your `do {} catch {}§ is outside the task? This is a function I use to get some JSON notes from my server:

func loadJsonNotesFromServer() {
		let url = URL(string: kFileLocationServer + kFileNameNotes + ".json")!
		let config = URLSessionConfiguration.default
		config.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData

		let session = URLSession.init(configuration: config)
		session.dataTask(with: url) {(data, response, error) in
			do {
				if let d = data {
					let data = try JSONDecoder().decode(Notes.self, from: d)
					DispatchQueue.main.async {
						self.notes = data.notes
					}

				} else {
					print("Error: No JSON notes data at url: \(url)")
					self.loadJsonDataFromBundle_Notes()
				}

			} catch {
				print ("Error loading JSON notes data from url: \(url)\nError: \(error)")
				self.loadJsonDataFromBundle_Notes()
			}
		}.resume()
	}

The only difference I can see is your do block is outside the task...?

URLSession.dataTask(with: URL) error: Type of expression is ambiguous without a type annotation
 
 
Q