I'm back once more with another issue. In my attempt to learn swift Ive decided that writing a bunch of small, random, playground scripts, that i can go back and use as references later on is a good place to start.
What I'm working on now, is a small script that uses 2 different ways to read the same file, printing the file contents when finished.
Using quite a few tips I found here on stack (aswell as the official docs) Ive managed to piece together a script that should do the above goal. (Please excuse the heavy commenting, as these are references)
//Import for use of NSString import Foundation //ATTEMPT #1 //Set file locations let location = NSString(string:"/Users/me/Desktop/file.txt").stringByExpandingTildeInPath //Make fileContent = data within file let fileContent = try? NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding) //print content of file print("The file contains:\n", fileContent, "\n") //ATTEMPT #2 //set filename let filename = "file.txt" //set document path let documentsPath = "/Users/me/Desktop" let destinationPath = documentsPath + "/" + filename //read file let data: NSData? = NSData(contentsOfFile: destinationPath) print("The file contains:\n", data, "\n")
I have created the file in the given location, Yet no matter how i try to work either bit of code, I recieve 'nil' as my file contents (which is simply 'hello world')
The file is a simple text file, UTF8 made with textwrangler. There is no free space before or after my string in the file. Any ideas would be greatly appreciated.