0

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)")
    }
}
JimmyJammed
  • 9,598
  • 19
  • 79
  • 146
  • You should work with NSURL instead of NSString paths – Leo Dabus Nov 22 '15 at 00:43
  • http://stackoverflow.com/a/27722526/2303865 – Leo Dabus Nov 22 '15 at 00:45
  • @LeoDabus Thanks! Using your code from the other post, do you know how to filter by directory name? I would prefer to just filter the directory named 'CSVs' than filter by pathExtension 'csv', as this seems safer in case other CSV files get added to the project outside of the desired folder. – JimmyJammed Dec 11 '15 at 14:40

1 Answers1

-1

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.
Daniel Zhang
  • 5,778
  • 2
  • 23
  • 28