gpt4 book ai didi

ios - 从类中调用父方法

转载 作者:可可西里 更新时间:2023-11-01 01:00:10 26 4
gpt4 key购买 nike

我对 Swift 和 iOS 开发还很陌生,所以请原谅使用的任何/所有陈述和术语......

我正在创建一个新的 Downloader 对象(只是为了下载 PDF 文件),我需要在我的 ViewController 中调用一个方法来显示文件已成功下载的消息。除了我正在做一些非常愚蠢的事情并且似乎无法让它工作。

我正在使用 Ahmet Akkök 在 this question 上的回答当我尝试使用 yourOwnObject.showDownloadCompleted() 时,它找不到该方法。

任何帮助都将不胜感激!

ViewController 中的代码:

override func viewDidLoad(){

super.viewDidLoad();
let pdfURL = "exampleToPFD.com/mypdf.pdf";

let url = NSURL(string: pdfURL);

let d = Downloader(yourOwnObject: self);
d.download(url!);

}

func showDownloadComplete(){
print("done");
}

The Downloader.swift 中的代码:

import Foundation

class Downloader : NSObject, NSURLSessionDownloadDelegate{
var url : NSURL?

// will be used to do whatever is needed once download is complete
var yourOwnObject : NSObject?
var downloaded = false;
var documentDestination = "";

init(yourOwnObject : NSObject){
self.yourOwnObject = yourOwnObject
}


//is called once the download is complete
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL){
//copy downloaded data to your documents directory with same names as source file
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first
let destinationUrl = documentsUrl!.URLByAppendingPathComponent(url!.lastPathComponent!)
let dataFromURL = NSData(contentsOfURL: location)
dataFromURL!.writeToURL(destinationUrl, atomically: true)

//now it is time to do what is needed to be done after the download

print("download done...");
// call to the parent method here

documentDestination = destinationUrl.absoluteString;
print("DestURL" + (destinationUrl.absoluteString));
}

//method to be called to download
func download(url: NSURL){

self.url = url

//download identifier can be customized. I used the "ulr.absoluteString"
let sessionConfig = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(url.absoluteString)
let session = NSURLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil)
let task = session.downloadTaskWithURL(url);

task.resume();
}
}

最佳答案

您可以使用委托(delegate)来实现这一点。您可以尝试以下

下载器.swift

import Foundation

protocol DownloadDelegate {
func downloadCompleted()
}

class Downloader : NSObject, NSURLSessionDownloadDelegate{
var url : NSURL?
var downloadDelegate : DownloadDelegate!
// will be used to do whatever is needed once download is complete
var downloaded = false;
var documentDestination = "";

//is called once the download is complete
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL){
//copy downloaded data to your documents directory with same names as source file
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first
let destinationUrl = documentsUrl!.URLByAppendingPathComponent(url!.lastPathComponent!)
let dataFromURL = NSData(contentsOfURL: location)
dataFromURL!.writeToURL(destinationUrl, atomically: true)

//now it is time to do what is needed to be done after the download

print("download done...");
downloadDelegate. downloadCompleted()

documentDestination = destinationUrl.absoluteString;
print("DestURL" + (destinationUrl.absoluteString));
}

//this is to track progress
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64){
print((String)(totalBytesWritten)+"/"+(String)(totalBytesExpectedToWrite));

}

// if there is an error during download this will be called
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?){

if(error != nil){

//handle the error
print("Download completed with error: \(error!.localizedDescription)");

}

}


//method to be called to download
func download(url: NSURL){

self.url = url

//download identifier can be customized. I used the "ulr.absoluteString"
let sessionConfig = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(url.absoluteString)
let session = NSURLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil)
let task = session.downloadTaskWithURL(url);

task.resume();
}
}

View Controller :

class ViewController: UIViewController,DownloadDelegate{
override func viewDidLoad(){

super.viewDidLoad();
// Do any additional setup after loading the view, typically from a nib.
let pdfURL = "exampleToPFD.com/mypdf.pdf";

let url = NSURL(string: pdfURL);

let d = Downloader();
d.downloadDelegate = self
d.download(url!);


showToast("Download Started...");

}

func downloadCompleted() {
//download completed
}
}

关于ios - 从类中调用父方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35292852/

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