【问题标题】:Downloading files into root app directory using Swift使用 Swift 将文件下载到应用程序根目录
【发布时间】:2014-08-29 21:46:41
【问题描述】:

我是 Swift 编码的新手,我对 Objective C 没有太多经验,所以我尝试从网络 (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
}


}

【问题讨论】:

    标签: ios swift nsurlsession


    【解决方案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
    }
    

    希望这行得通。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2019-04-13
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多