Swift 2.0
If you have been using the old way of Swift 1.2 to locate files in your local directory and documents folder, you can forget about that in Swift 2.0 in the latest Apple update of iOS 9.1 and Xcode 7.1.
Before you would use something like:
1 |
documentsDirectory().stringByAppendingPathComponent("somefilename") |
After I had to do some headbanging I finally got it to work, so here is the completed solution, in hoping that you don’t waste as much time as I did :
1 2 3 4 5 6 7 8 9 10 11 |
func getDocumentsURL() -> NSURL { let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] return documentsURL } func fileInDocumentsDirectory(filename: String) -> String { let fileURL = getDocumentsURL().URLByAppendingPathComponent(filename) return fileURL.path! } |
So now I can get my file path with ‘fileInDocumentsDirectory(“somefilehere”) ‘
Keep in mind that I only use ‘.path’ because I already have other things using the path, so I am being lazy on changing these. However Apple is recommending that everyone starts using NSURLS instead.
Cheers !
Thank you. How do I load the content of the file? for example the text from a text document to a label.
I had it working before Xcode 7 :-/
Here it is mate, I am guessing you need it for something like this, so I made a little article for it:
http://www.helpmecodeswift.com/advanced-functions/saving-and-loading-text-in-swift-2-0