NSKeyedArchiver/Unarchiver

Lately I've spent some time wrtiting an app for myself. It is supposed to let you create tasks, mark them as done/undone and then track your progress. I've called it "Habit Tracker" and it is available here. While writing this utility I came across a few interesting issues and this blogpost will cover one of them.

Saving and Loading data

The feature that "Habit tracker" surely needed was ability to store and load users tasks. Core data seemed a bit overkill for me, so I decided to use NSKeyedArchiver/Unarchiver.

NSCoding

First of all lets create a class which we want to archive. It will be called Task. In order to save and load it with use of NSKeyedArchiver/Unarchiver we have to make our class implements two methods provided by protocol .

Now lets create our example Task class.

Now we are able to properly store our tasks in files.

Saving

Loading

If we use iOS Simulator, all files that are saved by us are stored in our app's folder. Using my saving code you will find file named "FILE_NAME" under:

/Library/Developer/CoreSimulator/Devices/{DEVICE_ID}/data/Containers/Data/Application/{APP_ID}/Documents

By using this command you can get your currently running iOS Simulator ID:

This article is cross-posted with my my personal blog

Blog