User defaults refers to a storage area for user preferences and other data. This tutorial shows how to save notes to user defaults.
- [Instructor] There are multiple ways in iOS to save data to persistent storage. One of those is to use something called User Defaults. While user defaults is not the most secure method to save data to a file in that it's not encrypted, it's a very easy system to setup and to implement within your applications. And you may find yourself using it for data that's not sensitive. So let's take a look at how to implement that in our code. I'm at the bottom of my code here right above did receive memory warning in ViewController.swift and I'm going to create a method called save and then one called load.
We're not going to write anything inside of these methods just yet. We want to save data whenever we make a change to our data. So if we're adding new note or deleting a note, that's a good time to save. So I'm going to add a save call inside of tableView commit editingStyle and then one where we add a note. So right at the bottom of add note, I'll save. We want to load the data when the application launches. So at the bottom of viewDidLoad, I'm going to load my notes.
Now let's define what the save and load methods actually do. First is save. In this method, we're going to save our data to persistent storage. We'll do that using UserDefaults.standard.setvalue for key. The one that we want is the one that has any with a question mark. This allows us to save any type of data. So pass that in and then the data is just going to be our data array. So type data and then you can tab over to the key area and the way the user defaults works is through key value pairs.
So we need to associate this data with a key which we'll use later on to extract the data in the load method. Type an open quote if you have string highlighted and it should wrap the string in quotes and then you can just type the string which I'm going to call notes. If you have more than one key, and usually just about any time it's a best practice to make a property and then use that property value here instead of a string of literal, but for what we're doing, I'm okay with using a string literal now. So inside the load method, the first thing I'm going to do is make sure there actually is data to load and it's the right type of data.
So type if and then let and we're going to call this loaded data. The data type is going to be a string array just like our data. And then it'll be equal to UserDefaults.standard.value forKey. Then we just pass in our key, it's a string. The key is notes just as we put above right here. And then we need to typecast that, as with a question mark, a string array so square brackets and then String with a capital S inside of there.
It might be a little easier if I hide the navigator so you can see it all in one line. After the closed square brackets, type an open curly brace. Press return. Let's talk about this line real quick. So we're defining this loaded data constant as this value. We also have this if before it. What this says is if this value that's being created right here is non-nil, then execute the code inside of the curly braces. And then in there we can use loaded data and we know that the value is not nil.
So what it's doing is it's getting the value from user defaults and it's trying to typecast it as a string array. The reason why you have the question mark after string is because this could possibly fail. The typecast doesn't always work. So if the data loads in and the typecast works, then we'll execute this code in the curly braces. So in that case, we can say data equals loaded data. And then we can reload the table view.
Because this load method is called at the end of view did load, the table view will already be loaded up and ready to go 'cause we're going to have to reload that data here. And that's why we're going to make the call to table.reloadData. So that's going to recall all of those methods that get data for the table view. And at this point we should be able to test the application and see that the data is indeed saving to persistent storage. So now let's run the application and see what we get.
So now the data's loaded up and we actually don't have any notes within the view. And what's happening here is this load method is called and it looks like it's getting this empty value as an empty string array. And that makes it kind of confusing with this string array that we have right here so I'm just going to delete everything inside of our data array, make a little bit more sense. So let's run the application again. And there we have it with no notes. We should be able to add notes and delete them and then hit Done and then what I'm going to do before actually quitting the application, I'm going to go to the home screen first.
I've just seen that it works more consistently is saving the data to persistent storage if I go to the home screen rather than essentially crashing the application by hitting that stop button. So we're going to hit home button first. Go there, hit stop, and then I actually don't have to run it again. I can actually just tab back into the simulator with command tab and then I should be able to open up plain old notes, and I can see in there that it did indeed save the data as expected. So we can save data to persistent storage using user defaults.
- Creating an Xcode project
- Configuring Xcode preferences
- Creating a table view controller
- Setting a data source for the table view
- Deleting, editing, and saving app data
- Working with multiple view controllers