简体   繁体   English

Swift-如何在自定义目录中列出文件?

[英]Swift - How to list files in custom directory?

I need to import a bunch of data from CSV files in my app. 我需要从我的应用程序中的CSV文件导入一堆数据。 I have a custom folder 'CSVs' that contains a bunch of '.csv' files. 我有一个自定义文件夹'CSVs',其中包含一堆'.csv'文件。 Since these files will change over time, I don't want to use static filenames, but instead just loop through the 'CSVs' folder and read in all .csv files. 由于这些文件会随着时间而变化,因此我不想使用静态文件名,而只是循环遍历“ CSVs”文件夹并读取所有.csv文件。

I tried this but no files are ever found: 我试过了,但是没有找到文件:

func importCSV() {
    let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
    let csvPath = (documentsFolderPath as NSString).stringByAppendingPathComponent("CSVs")
    let filemanager = NSFileManager()
    let files = filemanager.enumeratorAtPath(csvPath)
    while let file = files?.nextObject() {
        print("File: \(file)")
    }
}

If files are not being seen by your code, either or both of the following cases could be true. 如果您的代码看不到文件,则以下两种情况之一或两种都可能成立。

  • There is something wrong with your path. 您的路线有问题。
  • There are no files at csvPath . csvPath上没有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM