0

I've added a bunch of files to my swift project folder like so:

enter image description here

I need to access these files at a certain point in the project for two purposes:

  1. Use the name of each file to populate Categories (ie. Age, Alone, Amazing...)
  2. Access the data within the files to populate each Category items page

I've implemented this code in the viewDidLoad of my class to access and iterate through the files

//get categories
let fileManager = NSFileManager.defaultManager()
let directoryPath = NSHomeDirectory() + "/Categories"
let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(directoryPath)!

while let element = enumerator.nextObject() as? String {
  println(String(element))
}

This does not work and produces the error fatal error: unexpectedly found nil while unwrapping an Optional value on the line let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(directoryPath)!. I suspect (and correct me if I'm wrong), that it is accessing the applications internal file structure and as a result cannot find the files specified. As I am new to IOS development, my general understanding of how this works is nebulous at best.

So How exactly do I access files I've added the project in the way I've shown in the image, so as to iterate through and get the data I'm looking for

EDIT

So I've changed the structure to make the Categories folder an actual folder like so:

enter image description here

I'm currently trying to use NSBundle to access the application route, however I am not sure how to do that. Any guidance would be appreciated.

jshbrmn
  • 1,737
  • 3
  • 22
  • 52

1 Answers1

1

The problem is that xcode's project navigator folders are not actual folders in the disk. So this:

let directoryPath = NSHomeDirectory() + "/Categories"

does not exist in your hdd. If you want this folder you have to navigate to your projects folder on your hdd, create it manually and add the files you want in it.

You can refer to a file and read it like this:

let htmFile = NSBundle.mainBundle().pathForResource("aboutText", ofType: "html")
let htmlString = NSString(contentsOfFile: htmFile!, encoding: NSUTF8StringEncoding, error: nil)

for json you can get a dictionary with

let jsonFile = NSBundle.mainBundle().pathForResource("aboutText", ofType: "json")
let dict = NSDictionary(contentsOfFile: jsonFile)
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • Thanks for the prompt reply. I am wondering though, I need to get the folder directory path, and loop through a list of files of a specific type. So in this case I do not know the names of the files only the types so `let htmFile = NSBundle.mainBundle().pathForResource("aboutText", ofType: "html")` won't work because it assumes I know the `pathForResource` – jshbrmn Mar 11 '15 at 16:14
  • Added also an example for parsing a json file to a dictionary. – Nikos M. Mar 11 '15 at 16:15
  • You know the files you have in the folder, so you can iterate through all of them. – Nikos M. Mar 11 '15 at 16:17
  • If you put your files within an actual folder in the disk you can use this answer to get their titles http://stackoverflow.com/questions/6398937/getting-a-list-of-files-in-the-resources-folder-ios – Nikos M. Mar 11 '15 at 16:22
  • The issue is with getting each file name programmatically, not manually. – jshbrmn Mar 11 '15 at 17:14
  • See my previous comment. http://stackoverflow.com/questions/6398937/getting-a-list-of-files-in-the-resources-folder-ios – Nikos M. Mar 11 '15 at 17:15
  • Thanks! Was wondering...how would `NSString * resourcePath = [[NSBundle mainBundle] resourcePath];` translate to swift? – jshbrmn Mar 11 '15 at 17:20
  • `var resource = NSBundle.mainBundle().resourcePath` – Nikos M. Mar 11 '15 at 17:23
  • Just got this myself. Thanks a lot for the help! – jshbrmn Mar 11 '15 at 17:25