I've added a bunch of files to my swift project folder like so:
I need to access these files at a certain point in the project for two purposes:
- Use the name of each file to populate Categories (ie. Age, Alone, Amazing...)
- 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:
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.