- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个采用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.
因此,您还需要实现 NSURLSessionTaskDelegate
和 NSURLSessionDelegate
协议(protocol)中的方法。
关于ios - swift 如何采用 NSURLSessionDownloadDelegate 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883599/
所以当我运行这个函数时 func run(u: String) { let URL = NSURL(string: u) let config = NSURLSessionConfig
我实际上是在处理下载/上传文件并跟踪进度。它运作良好。但是,我只是想知道如果出现问题怎么办,而且我在该委托(delegate)中没有看到任何处理错误的方法。以下是 NSURLSessionDownlo
我想创建一个采用NSURLSessionDownloadDelegate协议(protocol)的类 我所做的是: class imageDelegate: NSURLSessionDownloadD
我是一名优秀的程序员,十分优秀!