簡體   English   中英

使用Swift將文件下載到根應用目錄

[英]Downloading files into root app directory using Swift

我是Swift編碼的新手,我對Objective C沒有太多的經驗,所以我嘗試從Web(csv)下載文件並將其轉儲到根程序目錄中。

不幸的是,盡管我正在通過ObjectiveC中的http://www.appcoda.com/background-transfer-service-ios7/進行該教程的學習,但是我在Swift中找不到任何使用方法的教程。

這可能是很基本的(道歉),但是我試圖在Swift中創建一個類,以替換ObjectiveC中FileDownloadInfo類的實現。 (如果有人有本教程的Swift示例,那將非常有幫助。

在ObjectiveC中的實現是:

@implementation FileDownloadInfo

-(id)initWithFileTitle:(NSString *)title andDownloadSource:(NSString *)source{
    if (self == [super init]) {
        self.fileTitle = title;
        self.downloadSource = source;
        self.downloadProgress = 0.0;
        self.isDownloading = NO;
        self.downloadComplete = NO;
        self.taskIdentifier = -1;
    }

    return self;
}

@end

然后通過以下方式填充FileDownloadArray

-(void)initializeFileDownloadDataArray{
    self.arrFileDownloadData = [[NSMutableArray alloc] init];

    [self.arrFileDownloadData addObject:[[FileDownloadInfo alloc] initWithFileTitle:@"iOS Programming Guide" andDownloadSource:@"https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/iphoneappprogrammingguide.pdf"]];
}

我已經在Swift類中創建了以下內容,但是當然沒有任何功能-如何修改它以使我能夠以與上述相同的方式填充數組?

import UIKit

class FileDownloadInfo: NSObject {

    var fileTitle: NSString
    var downloadSource: NSString
    var downloadTask: NSURLSessionDownloadTask?
    var taskResumeData: NSData?
    var downloadProgress: Double
    var isDownloading: Bool
    var downloadComplete: Bool
    var taskIdentifier: Int

init(initWithFileTitle title: NSString, andDownloadSource source: NSString) {
        self.fileTitle = title
        self.downloadSource = source
        self.downloadProgress = 0.0
        self.isDownloading = false
        self.downloadComplete = false
        self.taskIdentifier = -1
}


}

我只閱讀了部分介紹,這是我的看法。 希望它能有所幫助。 為了直接翻譯,您可以嘗試:

func initializeFileDownloadDataArray(){
    arrFileDownloadData = [FileDownloadInfo]()
    arrFileDownloadData.append(FileDownloadInfo("iOS Programming Guide", downloadSource:"https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/iphoneappprogrammingguide.pdf" ))
}

如果我正確閱讀,則初始化程序的格式類似於以下格式:

init(title: String, downloadSource: String) {
    self.fileTitle = title
    self.downloadSource = source
    self.downloadProgress = 0.0
    self.isDownloading = false
    self.downloadComplete = false
    self.taskIdentifier = -1
}

希望能奏效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM