Read Write and Delete — File Handling from XCode Playground

Varindra Hart
4 min readSep 29, 2016

--

2/11/18: Updated to Swift 4 / Full Playground code at the bottom

Every Playground has the ability to access a shared documents directory from which you can read and store files via the Playground.

This article is split into a few sections:

  • Set Up
  • Accessing
  • Reading
  • Writing/Updating
  • Deleting

So, just scroll down to the one you’re looking for if you’ve already done the set up.

Set Up

According to Apple:

Important: You must create the ~/Documents/Shared Playground Data/ directory before using this constant. Xcode doesn’t create the directory for you.

So, make sure to create a folder in your Documents directory, called Shared Playground Data, in case you haven’t already.

You can do this from Terminal, just do the following:

  1. move to your home directory (you can just do cd and you’ll be there)
  2. cd Documents
  3. mkdir “Shared Playground Data” or mkdir Shared\ Playground\ Data (doesn’t matter which one you do)

That’s it.

Accessing

To access the directory URL in your Playground:

  1. import PlaygroundSupport
  2. Use the Playground constant playgroundSharedDataDirectory

Reading

If you have your own file in the Shared Playground Data directory, then just replace “testFile.txt” with the name of your file.

Also, if you aren’t really concerned with catching errors, for error handling, you can simply one-line the read with `try?`

Writing/Updating

If you’re wondering about the `atomically` parameter, that is just asking whether or not the write operation should be an all or nothing operation — as explained by this stack overflow post.

It’s important to note that writing to the same file will not update the file by appending. Instead it will overwrite the the original file. So, simply writing to file twice will not allow you to perform updates. The following snippet explains updating by copying the contents and updating it an inefficient process:

As you can see, we gather the contents of the file first and then append data to the contents. Once all modifications are complete, you can then write the file. While this works, it is very inefficient as your file grows since it reads all of the data before every update.

We can use FileHandle to make this more efficient:

Updating with FileHandle

Updating file contents using a FileHandle

To avoid reading all of our file data we can take advantage of FileHandles convenient seekToEndOfFile() function which when called will cause all updates to start from end of the file. There is a similar function, seek(toFileOffset:), which lets the caller move editing to any position within the file by supplying an offset — we won’t be using this as we just need to append to the end of the file, but it’s worth mentioning. Once we convert our new content to data and write it, we close the file and that’s it!

FileHandle Documentation 👍🏽

Deleting

To delete a file we use the FileManager to remove the file at the URL we specify. The line below the do-try-catch block shows that the file has been deleted.

For more on FileManager check this out: FileManager Documentation

Source Code

⭐️ Full playground code gist here

Well, that’s it in a nutshell. Thanks for reading and if you want feel free to drop a comment saying what you’ve used file handling to do!

--

--

Varindra Hart

I’m Varindra, a math teacher turned iOS developer. I love math, animals, and making apps. Join me on my tech adventure!