Hey there!
In this tutorial, we're looking at how to read and write NSUserDefaults.
The code in this tutorial is tested on iOS 9 and Swift 2.0.
Anything else, and your milage may vary.
Most applications need to store a little bit a data.
Not enough to require a full blown database, just a small amount of user data.
A really good example for NSUserDefaults is user settings for your app.
We can store our name, favorite beer, and profile pic in the defaults.
Let's see how'd we'd do that!
NSUserDefaults is a user and app specific database.
That means we can't read the defaults from another app.
It's tied to one app only.
Well that's partially true.
We can create user defaults for a suite of applications, in which case, all the apps
in the suite can share the the defaults.
We're using the standard user defaults in this tutorial, so it's tied to just our app.
We can't read defaults for other users of course.
That's on their phone.
And database is really a strong word here.
It's more like a dictionary that automatically persists for you.
I think of the NSUserDefaults as an app specific bag we persist things into, and we can get
at a later time.
Here's all the different types we can store in NSUserDefaults.
As you can see, we can store pretty much anything in there, but that doesn't mean it's a good
idea.
Don't use this to store sensitive information like passwords and such.
Imagine this file is on an open server that any script kiddie could hack into.
Sensitive stuff goes into the keychain, or an encrypted database.
We also don't want to use this for storing large files.
It'd be really slow, and there are better storage solutions for that.
But let's assume we're building an app that stores
a profile picture, and our favorite beer.
We also want to show the user the last time this data has changed.
For the profile picture, we'll let the user select any image in the photo album.
Any image selection will automatically update the photo stored.
And we'll take any text for a favorite beer type, and store it when the user presses the
"Save beer" button.
To start, we'll get a reference to the standard user defaults.
Let's start with the favorite beer.
This example is the most straight forward.
We just call set setObject with the string we want to store, and the string key we want
to use to get it later.
Notice we're using a constant for the key.
This is a practice we want to make a habit.
If we hard code the constants in our code, it's very easy to screw up the case, or mistype
the string.
If we use a constant, the compiler will let us know if we typed it wrong.
To get it back out, we call stringForKey.
It's kinda weird that it goes in as an object and back out as a string.
Especially since strings are structs, but that's where we're at.
If we're setting an integer, we'd use set setInteger and getInteger.
See?
Kinda wonky.
So strings are objects in, and strings out.
Easy.
We also wanted to store an image.
We'll use a UIImagePickerController to get the image.
To save it, we need to get the image data.
We'll get the JPEG representation of the selected image, and store that.
To get it back out, we'll use the method objectForKey.
What we get back is NSData, just like what we stored.
We'll initialize a UIImage with the data, and then display it in a UIImageView.
So that's images in and out.
The last thing we wanted to do was save a timestamp of when the user modified the defaults.
Dates are NSDate objects, so we use the same objectForKey method we used for the images.
This is simpler, because we don't need to do anything special with the object before
saving.
NSUserDefaults knows it's a date.
When we pull it back out, we can get it as a date.
Once we have the date object, we can use a formatter to display it anyway we want.
And that's how NSUserDefaults works.
If you have any questions, let me know in the comments below.
The code for this tutorial is available on GitHub.
Here's the address, and I'll include it in the description.
If there's Swift topics you'd like covered, again let me know in the comments.
Please subscribe if you liked the tutorial.
New ones come out each week, and subscribing helps make sure you don't miss any tutorials.
Thanks for watching, and I'll see you in the next video!