[MUSIC] I'll come over here, and I'll say this new function, which we'll implement in a second. Self dot save hobbies to user defaults. User defaults, and it's user defaults kind of works like a dictionary that lives in the file system on your phone. So it's not im memory. it's written quote on quote two disc. The flash storage of your phone or simulator, right? So whenever we update my hobbies, we'll save those hobbies to our user defaults let's go and implement that function So we'll put that at the bottom here. By the way in Swift, the order of where your functions are in your actual code source file doesn't matter anymore. So in other program languages, before we call a function inside of. Another function perhaps, we have to put the definition for that function physically above that line of code. So if that's not the case, you're more free to choose how your code is organized. All right so, inside of our save hobbies, c is our default function. We're going to take our array of hobbies, my hobbies. And we're going to save that into data, meaning NS data, and we'll do that with this, with this keyed NS keyed art class. All right. Now in order for that work we have to make the hobby class conform to the NS coding protocols, so that we can take the properties in the And the hobby, and turn them into data. And then, we can also be able to take the data and turn it back into a hobby. We'll look at that in a little while, when we finish implementing the models and data providers, but let's continue for now. Once we've saved that array of my hobbies into data, which we're calling hobby data, here. Then we can save that data to the disc. And, we'll do that again using NSN user defaults. So, we'll say userdefaults.standarduserdefaults .setValue our hobbyData for the key MyHobbies. We can't just put the MyHobbies erase straight into in its user defaults because the type of MyHobbies is an array of that except type Hobby. For in its user defaults you have to use what are called p-list objects which means it has to be, and NS something, something object. Some NS objects have class like NSArray, NSDictionary, NSString, NSNumber, ect, ect. So changing it to NSData satisfies that requirement for us which is why we go through the step of turning our hobby array into a type of data. All right. So now, we can save it into that key, and then we want to make sure that it actually saves it to disk. So we need to call this line here, NSUserDefaults.standardUserDefaults().syn- chronize. If you don't call synchronize, the next time that you start your application t his data will no longer be inside of the key MyHobbies. So make sure to actually erase it to the disk by calling this only works if ns user defaults knows there's a key called my hobbies in which to save this hobby data value. So we need to register Register the MyHobbies key. And a good place to do registration of NS user defaults keys is inside of your app delegate's application didFinishLaunchingWithOptions function. So let's go do that. All right, so here we are in app delegate and we're going to go register that MyHobbies key. But we only want to do that if there's nothing in the my hobbies care already, because we don't want to bother overwriting. Or we don't want to overwrite, any data that we saved over there on purpose. So I'll say if, and it's user defaults.stan. Then I want to say NSUSERDefaults.StandardUserDefaults, register defaults, my hobbies, and I need to provide some value for that, some initial value for that hobbies key which is why you see hobby data again here. So I'm just going to create a fictitious array of myHobbies that will be the default for a new user, let's say. Again, we haven't implemented the hobbyName initializer function yet. We'll do that in the next lesson when we Work on the model class so we'll let this error just stand here for now. But we've created an array of hobby's using that custom initializer which we'll array and then we do the same thing. We save it into data and now we can see that hobby data as the initial data for the my hobbies key. And again we must call synchronized to save that initial default value into its users defaults. [MUSIC]