简体   繁体   中英

Swift - How to list files in custom directory?

I need to import a bunch of data from CSV files in my app. I have a custom folder 'CSVs' that contains a bunch of '.csv' files. 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.

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 .

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.

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