简体   繁体   中英

swift how to adopt to NSURLSessionDownloadDelegate protocol

I want to create a class that adopts to NSURLSessionDownloadDelegate protocol

what i did is:

class imageDelegate: NSURLSessionDownloadDelegate  {
}

that is easy and it is fine. but i wanted to add one of it s method. i did this:

class imageDelegate: NSURLSessionDownloadDelegate  {
     @objc func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
        print("asdfasf")
    }
}

then i got error:

 Type 'imageDelegate' does not conform to protocol 'NSObjectProtocol'

any idea how to solve that please?

I usually use the calls that take completion blocks, so I'm not that familiar with using an `NSURLSessionDownloadDelegate'.

The error you're getting says you need to conform to the NSObject protocol, which tells me you need to make your class a subclass of NSObject. That will get you past the current error you're getting.

Take a look at the docs for the NSURLSessionDownloadDelegate protocol. It looks like there are 3 methods in the protocol.

EDIT: Looking at the header for the protocol, only thedidFinishDownloadingToURL method is required. The other 2 are optional.

However, If you take a look at the docs, they say:

The NSURLSessionDownloadDelegate protocol defines delegate methods that you should implement when using NSURLSession download tasks. In addition to these methods, be sure to implement the methods in the NSURLSessionTaskDelegate and NSURLSessionDelegate protocols to handle events common to all task types and session-level events, respectively.

So you also need to implement the methods from the NSURLSessionTaskDelegate and NSURLSessionDelegate protocols.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM