NSURLSession dataTaskWithRequest:completionHandler: crashes

Originator:info
Number:rdar://20084869 Date Originated:07-Mar-2015 07:06 PM
Status:Open Resolved:
Product:iOS SDK Product Version:8.1
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:
Invoking - [NSURLSession dataTaskWithRequest:completionHandler:] results in a crash.

Steps to Reproduce:
Compile the following code:

let session = NSURLSession()
let request = NSURLRequest(URL: NSURL(string: "http://google.com”)!)

let task = session.dataTaskWithRequest(request) { _ in }

Expected Results:
Code runs fine.

Actual Results:
App crashes with the following exception:
-[NSURLSession dataTaskForRequest:completion:]: unrecognized selector sent to instance 0x7fa03981c070.

Notes:
Explicitly specifying completionHandler: as the parameter name instead of using the trailing closure syntax results in the same crash.

Comments

Workaround

It looks like this error is caused by using the default NSObject constructor on NSURLSession. If you construct your NSURLSession this way, it works:

let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!