Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 353 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No exact matches in call to instance method error message in Swift

#1
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**
Reply

#2
# Why Xcode Yelling?
Maybe the message text seems a little bit **self-explanatory** but because [Xcode][1] 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][2]** instead of the [NSMutableURLRequest][3].


## 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.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#3
Also check if the `url` is the `actual url` and not a `String`

URLSession.shared.dataTask(with: url) { data, response, error
Reply

#4
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.
Reply

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

```lang-swift
Text("Leading text \(String(describing: value))")
```
<sub>[Source](

[To see links please register here]

;
Reply

#6
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
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through