gpt4 book ai didi

ios - swift 如何采用 NSURLSessionDownloadDelegate 协议(protocol)

转载 作者:行者123 更新时间:2023-11-30 13:54:05 28 4
gpt4 key购买 nike

我想创建一个采用NSURLSessionDownloadDelegate协议(protocol)的类

我所做的是:

class imageDelegate: NSURLSessionDownloadDelegate  {
}

这很简单,也很好。但我想添加其中一种方法。我这样做了:

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

然后我得到了错误:

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

知道如何解决这个问题吗?

最佳答案

我通常使用需要完成 block 的调用,因此我不太熟悉使用“NSURLSessionDownloadDelegate”。

您收到的错误表明您需要遵守 NSObject 协议(protocol),这告诉我您需要使您的类成为 NSObject 的子类。这将使您克服当前遇到的错误。

查看 NSURLSessionDownloadDelegate 协议(protocol)的文档。看起来协议(protocol)中有3个方法。

编辑:查看协议(protocol)的 header ,仅需要 thedidFinishDownloadingToURL 方法。其他 2 个是可选的。

但是,如果您查看文档,他们会说:

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.

因此,您还需要实现 NSURLSessionTaskDelegateNSURLSessionDelegate 协议(protocol)中的方法。

关于ios - swift 如何采用 NSURLSessionDownloadDelegate 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883599/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com