[英]Missing argument for parameter 'responder' in call swift
Ok so Im going a little crazy here trying to understand where is the problem with my code. 好的,我在这里疯了,试图了解我的代码的问题所在。 Im trying to make separate classes communicate so I could handle UI elements during file download progression.
我试图让单独的类进行交流,以便在文件下载过程中处理UI元素。
I went for a protocol solution that seems to fit my needs perfectly: 我选择了一种似乎完全符合我的需求的协议解决方案:
the protocol: 协议:
protocol DownloadResponder : class {
func downloadFinished()
}
the download class: (for the sake of the question I only display the download_zip
func and the didFinishDownloadingToURL
delegate) 下载类:(出于问题的考虑,我仅显示
download_zip
函数和didFinishDownloadingToURL
委托)
import UIKit
import Foundation
typealias CompleteHandlerBlock = () -> ()
class fileDownloader: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
//responder
var responder : DownloadResponder?
init(responder : DownloadResponder) {
self.responder = responder
}
var session: NSURLSession!
var handlerQueue: [String : CompleteHandlerBlock]!
// class var sharedInstance: fileDownloader {
// struct Static {
// static var instance : fileDownloader?
// static var token : dispatch_once_t = 0
// }
//
// dispatch_once(&Static.token) {
// Static.instance = fileDownloader()
// Static.instance!.handlerQueue = [String : CompleteHandlerBlock]()
// }
//
// return Static.instance!
// }
func download_zip(sURL: String, destination:String, name:String, fileis:Int) {
var session:NSURLSessionTask!
var sessionConfiguration:NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.visi")
sessionConfiguration.HTTPMaximumConnectionsPerHost = 5
self.session = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
var url = NSURLRequest(URL: NSURL(string: sURL)!)
var downloadTask:NSURLSessionDownloadTask = self.session.downloadTaskWithRequest(url)
downloadTask.resume()
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
println("session \(session) has finished the download task \(downloadTask) of URL \(location).")
responder?.downloadFinished()
}
the downloadFinished func in my FileInfo ViewController : 我的FileInfo ViewController中的downloadFinished函数 :
func downloadFinished() {
downloadLbl.text = "Downloaded"
println("DOWNLOAD OVER")
}
and finally, the function in my FileInfo ViewController : 最后是我的FileInfo ViewController中的函数:
func downloadFile(sender:UIButton!)
{
// some code...
fileDownloader().download_zip(datastring, destination: path, name: naming, fileis: self.fileId)
}
this func call fileDownloader().download_zip(datastring, destination: path, name: naming, fileis: self.fileId)
in my view controller triggers an error I didn't have before I add the protocol. 我的视图控制器中的此func调用
fileDownloader().download_zip(datastring, destination: path, name: naming, fileis: self.fileId)
触发了我在添加协议之前没有的错误。 It says: Missing argument for parameter 'responder' in call
. 它说:
Missing argument for parameter 'responder' in call
。 I can't think of any solution so if someone knows what it is please help! 我想不出任何解决方案,所以如果有人知道这是什么,请帮忙!
In your init
method of fileDownloader
you are expecting a parameter responder: DownloadResponder
which you are not providing when you are initializing the fileDownloader
in the downloadFile
method. 在
fileDownloader
的init
方法中,您需要一个参数responder: DownloadResponder
,当您在downloadFile
方法中初始化fileDownloader
时未提供该参数。
So instead of: 所以代替:
fileDownloader().download_zip(...
Do: 做:
fileDownloader(DownloadResponderImplementation()).download_zip(...
This assumes you to also Implement the DownloadResponder protocol as for example: 假设您还实现了DownloadResponder协议,例如:
class DownloadResponderImplementation: DownloadResponder {
func downloadFinished() {
//Do something here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.