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()
Reported via feedbackassistant.apple.com: FB7717686
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.
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.
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.
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.
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
}
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With