This works on my machine. Using Xcode 11.6 I created a new command-line tool project, changed main.swift to the below, and it compiles just fine.
import Foundation
func main() {
let selector = #selector(URLSession.dataTask(with:completionHandler:) as (URLSession) -> (URLRequest, @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask)
print(selector)
}
main()
When I run it, it prints:
dataTaskWithRequest:completionHandler:
I suspect thereâs something about your context thatâs messing things up, and itâs hard to say what without more context.
Having said that, please donât swizzle methods on classes you donât âownâ. NSURLSession was not designed to be patched in this way and, in my experience, patches like this tend to cause problems in the long term. If youâre lucky, theyâre problems like this one, that show up at compile time. If youâre less lucky, they show up at runtime.
Thanks for taking a look at it. Did you run the code from Xcode or was it from the Terminal app? I'm able to run the code from the command line fine, so I'm suspecting it could be related to my project settings, or perhaps to Xcode using a different version of the static analyzer. FYI, I'm using swizzling to test a class that uses URLSession, so it should be perfectly fine since production code will not be using the patched class.
Did you run the code from Xcode or was it from the Terminal app?
Quoting myself here:
Using Xcode 11.6 I created a new command-line tool project
I recommend that you do the same, just to confirm that your Xcode and my Xcode agree on the basics. After that you can start diffâing your working command-line tool project against your failing main project.
Note My best guess here is that youâve added an extension to URLSession in your main project and itâs that extension thatâs triggering the failure.
FYI, I'm using swizzling to test a class that uses URLSession, so it
should be perfectly fine since production code will not be using the
patched class.
Thatâs great news, although personally I avoid this complexity by adding a hook to the code under test that lets by substitute my test network subsystem in place of the real one.
I added a command line tool target to my project with the snippet to try the selector and it compiles ok. Now I'm adding new targets to replace the old ones generated by the swift command line tool. It seems to me it's swift CLI build settings that were given to the project the ones giving the error I mentioned. Thanks for your suggestion.