2
我試圖使用委託方法作爲文件使用NSURLSession
下載到更新進度條,但我似乎無法獲得委託方法調用。NSURLSession期間不叫URLSession委託下載斯威夫特
委託方法我是在斯威夫特(文件下載啓動時不會被調用)如下:
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64!, totalBytesExpectedToWrite: Int64){
println("delegate called")
if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
println("Unknown transfer size")
} else {
let index: Int = self.getFileDownloadInfoIndexWithTaskIdentifier(downloadTask.taskIdentifier)
let fdi: FileDownloadInfo = self.arrFileDownloadData.objectAtIndex(index) as FileDownloadInfo
NSOperationQueue().addOperationWithBlock({
//Calculate the progress
fdi.downloadProgress = Double(totalBytesWritten)/Double(totalBytesExpectedToWrite)
// Update the progressview bar
self.progressView.progress = Float(fdi.downloadProgress)
})
}
}
等效Objective-C的呼叫我想在斯威夫特複製以上(它被調用時,文件下載啓動):
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
NSLog(@"Delegate called");
if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
NSLog(@"Unknown transfer size");
}
else{
// Locate the FileDownloadInfo object among all based on the taskIdentifier property of the task.
int index = [self getFileDownloadInfoIndexWithTaskIdentifier:downloadTask.taskIdentifier];
FileDownloadInfo *fdi = [self.arrFileDownloadData objectAtIndex:index];
[.............]
}];
}
}
我感應,我做錯了什麼。雖然,在這兩種情況下,我沒有得到自動填充與我需要的變量填寫訪問(例如didWriteData
,bytesWritten
等),在Ob在我輸入-(void)URLSession:(NSURLSession *)session
後,我得到downloadTask
和didWriteData
等作爲選項。但是,使用Swift我沒有得到這些,所以我認爲我做錯了什麼。
在此先感謝您的幫助。