为什么尝试使用SwiftCSV解析下载的.csv无法找到文件?

时间:2016-04-27 01:50:09

标签: ios swift csv realm

所以我正在编写Swift应用程序,使用Alamofire,SwiftCSV和Realm [尝试使用Realm]并且我已成功从我的服务器下载.csv,但是当我尝试解析.csv时甚至只看到标题,它会抛出我的do {} catch {}

的catch语句

我的最终目标是让Alamofire下载.csv> SwiftCSV将其解析为可读对象[~~ 200]>然后填充Realm DB

你还可以看到在.response()输出之前抛出了catch {} ...有人能解释一下这个吗?是在不同的线程或什么?

疯狂的是,即使应用程序启动,该文件仍然存在,所以我想知道为什么它根本看不到它。

我只是不了解CSV库方法吗?

的ViewController:

var localPath: NSURL?
Alamofire.download(.GET, "http://wristaroo.com/schedule/wristaroo.csv", destination: { (temporaryURL, response) in
    let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let pathComponent = response.suggestedFilename

    localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)

    return localPath!
}).response { (request, response, _, error) in
    print(response)
}

var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as String

filePath = (filePath as NSString).stringByAppendingPathComponent("wristaroo.csv")

let filePathURL = NSURL.fileURLWithPath(filePath)
print("\noutside filePath: \(filePath)")

do {
    let csv = try CSV(url: filePathURL)
    print("test")

    csv.enumerateAsArray({ (array) in
        print(array.first)
    })
} catch {
    print("error outside")
}

var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as String

filePath = (filePath as NSString).stringByAppendingPathComponent("wristaroo.csv")

let filePathURL = NSURL.fileURLWithPath(filePath)
print("\noutside filePath: \(filePath)")

do {
    let csv = try CSV(url: filePathURL)
    print("test")

    csv.enumerateAsArray({ (array) in
        print(array.first)
    })
} catch {
    print("error outside")
}

控制台输出:

outside filePath: /Users/michaeldvinci/Library/Developer/CoreSimulator/Devices/522F6430-CB63-4607-89B8-6B3B4D9A3100/data/Containers/Data/Application/4DAA3C89-FF7E-4A0F-B251-4ED1AAF34C25/Documents/wristaroo.csv
error outside
Optional(<NSHTTPURLResponse: 0x7fe648f1eae0> { URL: http://wristaroo.com/schedule/wristaroo.csv } { status code: 200, headers {
    "Accept-Ranges" = "bytes, bytes";
    Age = 0;
    Connection = "keep-alive";
    "Content-Length" = 6909;
    "Content-Type" = "text/plain";
    Date = "Wed, 27 Apr 2016 01:40:11 GMT";
    "Keep-Alive" = "timeout=30";
    Server = "Apache/2";
} })

1 个答案:

答案 0 :(得分:1)

Alamofire.download()方法异步下载文件。因此,该方法在下载完成的文件之前立即返回。因此,您尝试在下载文件之前读取文件。

要修复您期望的行为,请将解析CSV逻辑移至download()方法的回调闭包。

如下所示:

Alamofire.download(.GET, "http://wristaroo.com/schedule/wristaroo.csv") { (temporaryURL, response) in
    do {
        let csv = try CSV(url: temporaryURL, encoding: NSASCIIStringEncoding)
        print("test")

        csv.enumerateAsArray { (array) in
            print(array.first)
        }
    } catch {
        print("error outside")
    }

    let fileManager = NSFileManager.defaultManager()
    let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let pathComponent = response.suggestedFilename

    return directoryURL.URLByAppendingPathComponent(pathComponent!)
}

此外,文件的字符编码是ASCII,因此您应该在读取文件时指定编码。