Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No exact matches in call to instance method error message in Swift

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

like image 781
mgyky Avatar asked May 29 '20 04:05

mgyky


4 Answers

Why Xcode Yelling?

Maybe message text seems a little bit self-explanatory but just because Xcode does not exactly point the parameter itself, a little bit hard to figurate for the first time.

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

Solution for the example case:

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.

Solution for a SwiftUI Example

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

    }

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

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.

like image 187
mgyky Avatar answered Sep 20 '22 15:09

mgyky


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 
}
like image 28
Simran Singh Avatar answered Sep 20 '22 15:09

Simran Singh


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

like image 27
Nelu Avatar answered Sep 21 '22 15:09

Nelu


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.

like image 42
Luke Avatar answered Sep 24 '22 15:09

Luke