31

I never get this before, What is the meaning of this error message in Swift:

No exact matches in call to instance method 'dataTask(with:completionHandler:)'

Here is my code block:

var request: NSMutableURLRequest? = nil
let task = URLSession.shared.dataTask(
            with: request,
            completionHandler: { data, response, error in
                DispatchQueue.main.async(execute: {
                  /// ...
                })
            })
task.resume()

Bug Report

Reported via feedbackassistant.apple.com: FB7717686

Note: The Error still exists in the Xcode Version 14.3 on 2023, May 1

MGY
  • 7,245
  • 5
  • 41
  • 74
  • 1
    The error message can be MISLEADING! For me I was passing **two** optionals as arguments for my function — while both needed to be non-optional. **If** I did correct one of the inputs then the error would instead be something meaningful: "Value of optional type 'String?' must be unwrapped to a value of type 'String'". The compiler is able to show the correct error message when a single parameter is passed incorrectly. Yet when it's two, the compiler gives you a misleading/hard to interpret message. FWIW some answers point to some other misleading error. So your case might be diff – mfaani Feb 06 '23 at 17:04

5 Answers5

34

Why Xcode Yelling?

Maybe the message text seems a little bit self-explanatory but because Xcode does not precisely point to the parameter itself, a little bit hard to figure out the first time.

Xcode is yelling because the method wants to see exact parameter types on the method call, that is easy.

Solution for the Question:

var request: URLRequest? = nil

let task = URLSession.shared.dataTask(
            with: request!,
            completionHandler: { data, response, error in
                DispatchQueue.main.async(execute: {

                })
            })
task.resume()

Just used the URLRequest instead of the NSMutableURLRequest.

A SwiftUI Case

Let's assume this is your UI:

        ZStack() {
            Image(systemName: "photo")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .background(Color.green)
                .foregroundColor(Color.white)
                .cornerRadius(12)
            Text(getToday())
                .font(.headline)
            }
        }

And this is the method that you're calling in the Text(...):

    func getToday() -> Any?
    {
        let now = Date()
        let calendar = Calendar.current
        let components = calendar.dateComponents([.day], from: now)
        return components.day

    }

Solution

In the example above solution would be changing the Any? to a String type.

A NSURL Case

if let url = NSURL(String: "http://example-url.com/picture.png") {
  // ...
}

Solution

if let url = URL(String: "http://example-url.com/picture.png") {
  // ...
}

ℹ️ No exact matches in call

to instance method '* * *'

This is a general error message for using the wrong type in the method calls. That's why I added here to help others.

I hope this answer will help some of you guys.

Best.

MGY
  • 7,245
  • 5
  • 41
  • 74
  • 6
    You might want to file a bug report with Apple. Surely the diagnostic could be better than this. – matt May 29 '20 at 05:07
  • 1
    Thank you @matt, I reported via https://feedbackassistant.apple.com. – MGY May 29 '20 at 05:40
  • 2
    The error still appears almost 2 years later in Xcode 12.4 in the same confusing way: I was using `session.dataTask(with: url)` but hadn't changed the surrounding function to pass the url yet (`url` was undeclared) and that's the error I got, even though `dataTask(with:) exists. – Neph Apr 27 '23 at 09:23
9

I was using dataTask with URL, but I was unwrapping the URL as NSURL, and that is why it was giving me an error.

I was doing this:

if let url = NSURL(String: "http://myyrl.com/filename.jpg") {
      //my code
}

What fixed the error was replacing NSURL with URL:

if let url = URL(String: "http://myyrl.com/filename.jpg") {
      //my code 
}
MGY
  • 7,245
  • 5
  • 41
  • 74
Simran Singh
  • 445
  • 6
  • 8
  • Thank you for your answer. It's about Xcode and it's a general error message. It can cause in so many situations. Best. – MGY Aug 07 '20 at 11:48
5

If you're getting this error in a Text element, try wrapping your value in String(describing: value). Fixed my case.

Text("Leading text \(String(describing: value))")

Source

Nelu
  • 16,644
  • 10
  • 80
  • 88
  • 1
    Might not be the exact answer to the question, but in general is a way to workaround this. Often it is just an unwrapped optional, and this way you will discover – brainray Nov 18 '21 at 11:38
3

Xcode gives this error when you add SKPaymentQueue methods (such as SKPaymentQueue.default().add() to your code before making the current class conform to the SKPaymentTransactionObserver protocol.

Just make your class conform to the SKPaymentTransactionObserver protocol to solve this error if this is the case.

Luke
  • 965
  • 8
  • 21
1

Also check if the url is the actual url and not a String

URLSession.shared.dataTask(with: url) { data, response, error
Naishta
  • 11,885
  • 4
  • 72
  • 54