Tim Dietrich

Custom Software Developer

Home Services Portfolio Blog About Contact Newsletter

Swift: Reading and Writing Files to the iOS File System

I'm using a simple caching technique in my ICD-10 app. When a user views an ICD-10 code, the code's details are stored in the Data Container portion of the app's Sandbox. This helps to improve performance and make some data available to the user when they are offline.

In this post, I'll briefly explain the iOS file system and show how you can write and read files to and from it using Swift.

The Library and Caches Directories

The location that I'm using to store the cached file is /Library/Caches. Apple describes the Library directory as a "top-level directory for any files that are not user data files." And while files stored in the Library directory is typically backed up (via iTunes), files in the Caches subdirectory are ignored. The Caches subdirectory is specifically designed to store data that you want to persist between sessions. For more information on the iOS file system, click here.

Writing Files

In Swift, writing the contents of a string to a file is very easy to do. The String data type supports a "writeToFile" method that takes the string and saves it to a specified path in the app's data container. For example, here's how I'm saving a string ("writeString") to the Library/Caches directory.


let writeString = "Hello, world!"

let filePath = NSHomeDirectory() + "/Library/Caches/test.txt"

do {
	_ = try writeString.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
	print(error.description)
}

Notice that I'm using the NSHomeDirectory Foundation function to get the path to the application’s home directory, and then simply appending "/Library/Caches" to it, along with the file name, to get the full path.

The call to the writeToFile method includes three parameters:
• The path to which the file is to be written.
• An "useAuxiliaryFile" option which, when True, guarantees that if the file already exists it won't be corrupted in the event of a system crash.
• And the encoding to use. In this example, I'm using NSUTF8StringEncoding.

Reading Files

Reading a file with Swift is also very easy. You can use the NSString class's "contentsOfFile" initialization option to load a file directly into a string variable. For example:


var readString: String
let filePath = NSHomeDirectory() + "/Library/Caches/test.txt"

do {
	readString = try NSString(contentsOfFile: filePath, encoding: NSUTF8StringEncoding) as String
	print(readString)
} catch let error as NSError {
	print(error.description)
} 

Once again, I'm using the NSHomeDirectory Foundation function to get the path to the application’s home directory, and then appending the remainder of the path to it. I use that, along with the NSString contentsOfFile method to initialize a variable, load the file's contents into it, and assign the value to String variable. In this example, I'm simply printing the file's contents.