1 Reply
      Latest reply: Feb 22, 2017 1:27 AM by eskimo RSS
      jarv81178 Level 1 Level 1 (0 points)

        Please help, I get teh following error in Swift 3:

         

        Ambiguous refernce to memer 'dataTask(wth:completionHandler)'

         

        
        URLSession.shared.dataTask(with: url) { (data, response, error) in
                    if error != nil {
                        print(error)
                        return
                    }
                }
        
        • Re: Ambiguous refernce to memer 'dataTask(wth:completionHandler)'
          eskimo Apple Staff Apple Staff (7,530 points)

          ‘Ambiguous reference’ errors like this one have two common causes:

          • You’re actually referring to something ambiguously

          • You have some other error in your code that’s thrown the compiler for a loop to the point where it generates this error incorrectly.

          In this case I believe you’re suffering from the latter.  I’ve seen similar problems when dealing with errors inside closures.

          Alas, I can’t point out the exact error because, when I put your code inside a small test project here in my office, it doesn’t generate the same error.  There’s clearly something context-specific in play here.

          My general approach for tracking down such errors is to comment out the code within the closure to get things compiling.  I then selectively uncomment code until I trigger the error.

          Alternatively, you can move the code from the closure out of its current context into a more vanilla context, fix the error, and then move it back.

          Regardless, once you track down the actual error it would be a good idea to file a bug about the poor diagnostic.  Please post your bug number, just for the record.


          Finally, just FYI, line 2 of your snippet would be better server by an if let:

          URLSession.shared.dataTask(with: url) { (data, response, error) in 
              if let error = error { 
          …
          

          That way, inside the if statement, you can access error as a non-optional.

          This isn’t the source of you ‘ambiguous reference’ problem, but it’s a good idea to fix it anyway.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"