class ImageTrack {
var session: NSURLSession?
var delegate: NSURLSessionDelegate?
var task: NSURLSessionDownloadTask?
func update() {
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
session = NSURLSession(configuration: config, delegate: delegate!, delegateQueue: NSOperationQueue())
}
func searchImage() {
if task != nil {
task?.cancel()
}
let url = NSURL(string: "http://www.raywenderlich.com/wp-content/uploads/2015/08/Screen-Shot-2015-08-20-at-12.27.21-am.png")!
task = session!.downloadTaskWithURL(url)
task?.resume()
}
}
I do not understand how it is possible that if the init of NSURLSession
asks me for a delegate of the type NSURLSessionDelegate
, I can call the methods of a child protocol as it would be with NSURLSessionDownloadDelegate
with the method:
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL).
EDIT My question is about the delegate variable of the NSURLSession which is of type NSURLSESSIONDELEGATE and that is not a NSURLSessionDownloadDelegate (although it is the other way around), so unless you cast it (delegate) you can not call NSURLSessionDownloadDelegate methods. So my question is: Is a cast internally in the code?