I'm new to coding for Swift and I don't have much experience of Objective C so am trying to download a file from the web (csv) and dump it into the root program directory.
Unfortunately I can't find any how-to tutorial in Swift although I am working through the tutorial at http://www.appcoda.com/background-transfer-service-ios7/ which is in ObjectiveC.
This may be really basic (and apologies) but I'm trying to create a class in Swift replacing the implementation of the FileDownloadInfo class in ObjectiveC. (If anyone has a Swift example of the tutorial, that would be REALLY helpful.
The implementation in ObjectiveC is:
@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
A FileDownloadArray is then populated via
-(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"]];
}
I've created the following in a Swift class, but of course there's no function - how do I amend this to enable me to populate an array in the same way as above?
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
}
}
I've only read part of the intro and this is my take on it. Hopefully, it can help. For the sake of direct translation, you can try:
func initializeFileDownloadDataArray(){
arrFileDownloadData = [FileDownloadInfo]()
arrFileDownloadData.append(FileDownloadInfo("iOS Programming Guide", downloadSource:"https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/iphoneappprogrammingguide.pdf" ))
}
If I read it correctly, the initializers would have a format like the one below:
init(title: String, downloadSource: String) {
self.fileTitle = title
self.downloadSource = source
self.downloadProgress = 0.0
self.isDownloading = false
self.downloadComplete = false
self.taskIdentifier = -1
}
Hopefully, that works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.