[英]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.