好的,所以我有点疯狂,试图了解我的代码问题在哪里。我试图让单独的类进行通信,这样我就可以在文件下载过程中处理UI元素。
我选择的协议解决方案似乎完全符合我的需求:
协议:
protocol DownloadResponder : class {
func downloadFinished()
}
下载课程:
(为了问题我只显示download_zip
func和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()
}
我的FileInfo ViewController中的downloadFinished func :
func downloadFinished() {
downloadLbl.text = "Downloaded"
println("DOWNLOAD OVER")
}
最后,我的 FileInfo ViewController 中的函数:
func downloadFile(sender:UIButton!)
{
// some code...
fileDownloader().download_zip(datastring, destination: path, name: naming, fileis: self.fileId)
}
我的视图控制器中的此函数调用fileDownloader().download_zip(datastring, destination: path, name: naming, fileis: self.fileId)
会在添加协议之前触发我没有的错误。它说:Missing argument for parameter 'responder' in call
。
我想不出任何解决方案,所以如果有人知道它是什么请帮助!
答案 0 :(得分:1)
在init
fileDownloader
responder: DownloadResponder
方法中,当您在fileDownloader
方法中初始化downloadFile
时,您需要一个参数fileDownloader().download_zip(...
所以而不是:
fileDownloader(DownloadResponderImplementation()).download_zip(...
执行:
class DownloadResponderImplementation: DownloadResponder {
func downloadFinished() {
//Do something here
}
}
这假设您还实现了DownloadResponder协议,例如:
dashboardPage(
dashboardHeader(
title = "Example of a long title that needs more space",
titleWidth = 450
),
dashboardSidebar( sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", icon = icon("th"), tabName = "widgets",
badgeLabel = "new", badgeColor = "green")
)),
dashboardBody(
# Also add some custom CSS to make the title background area the same
# color as the rest of the header.
tags$head(tags$style(HTML('
/* logo */
.skin-blue .main-header .logo {
background-color: #f4b943;
}
/* logo when hovered */
.skin-blue .main-header .logo:hover {
background-color: #f4b943;
}
/* navbar (rest of the header) */
.skin-blue .main-header .navbar {
background-color: #f4b943;
}
/* main sidebar */
.skin-blue .main-sidebar {
background-color: #f4b943;
}
/* active selected tab in the sidebarmenu */
.skin-blue .main-sidebar .sidebar .sidebar-menu .active a{
background-color: #ff0000;
}
/* other links in the sidebarmenu */
.skin-blue .main-sidebar .sidebar .sidebar-menu a{
background-color: #00ff00;
color: #000000;
}
/* other links in the sidebarmenu when hovered */
.skin-blue .main-sidebar .sidebar .sidebar-menu a:hover{
background-color: #ff69b4;
}
/* toggle button when hovered */
.skin-blue .main-header .navbar .sidebar-toggle:hover{
background-color: #ff69b4;
}
')))
)
)