No exact matches in call to instance method error message in Swift
Asked Answered
Q

5

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

Quill answered 29/5, 2020 at 4:34 Comment(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 diffDanziger
Q
36

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.

Quill answered 29/5, 2020 at 4:34 Comment(3)
You might want to file a bug report with Apple. Surely the diagnostic could be better than this.Befoul
Thank you @matt, I reported via feedbackassistant.apple.com.Quill
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.Laynelayney
F
10

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 
}
Feucht answered 7/8, 2020 at 7:30 Comment(1)
Thank you for your answer. It's about Xcode and it's a general error message. It can cause in so many situations. Best.Quill
B
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

Boulanger answered 13/10, 2021 at 15:22 Comment(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 discoverHedve
H
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.

Homeostasis answered 28/11, 2021 at 2:43 Comment(1)
My class was missing SKPaymentTransactionObserver, this was the only helpful comment, thanks!Ranit
U
1

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

URLSession.shared.dataTask(with: url) { data, response, error
Utter answered 4/10, 2022 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.