No exact matches in call to instance method error message in Swift
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
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.
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
}