Conforming User Class to NS Coder

Amir J
A free video tutorial from Amir J
Instructor
4.5 instructor rating • 19 courses • 87,421 students

Lecture description

We need to conform our custom class to NSCoder to be able to save it to disk using plist file.

Learn more from the full course

iOS with Swift & Firebase

Developing a complete iPhone App with Swift in XCode and with Firebase backend

05:49:31 of on-demand video • Updated November 2017

  • iOS interface Design
  • Database Structure local and cloud
  • Firebase Backend
English [Auto] Will come back before he could actually write or talk to a local disk to find out which technology you want to use for it there are multiple different ways that you could write something like this of one of them is converting your data such as let's say it list soullessness in one array and dictionaries and writing it as a text string and reading it might the other one is using. Hold that thought which is the framework from opon and another one is using the propertyless touching you're in for that propertyless propertyless actually really cool because they allow you to ask for information such as streams of Orlean's arrays or dictionaries and they're really really handy to read and write information. The only downside to be propertyless is you have to make sure your classes for at least the ones you want to write. They conform to N.S. coding protocol. Now let's quickly talk about that. Let's say I want to go to my User class and what they want to do is I want to be able to write this to my local. This is essentially no way that you can write a class across some class you're like this simply because there is no there is no predefined method for that. So what you want to do is you can do either convert this into a dictionary manually or use and coding to her lead and it's according to combat for you. That's what they're going to do. In this example. So what you want to do is you want to go ahead to your class declaration you want to say and that's coding. So that's the protocol I also want to use. And the moment you use that is going to give you a little error or saying well you want to use it but you are not using all of its necessary methods. And those are two functions essentially. One of them is in code. The other one is essentially the code so that McCarthyist to query that the them off my code. What makes more sense for me to have them in here. It doesn't really matter where they are located but it just it just makes more sense to me. So the first thing you want to do is then when you want to encode our information that means take this information convert it into something that can be written such as a dictionary. So it's actually quite easy. I'm going to say oh their dot and code. What do we want to quote. The one thing called the self-taught name for the key name. Very similar to when you define When you define dictionaries. So you say and quote this guy for that name. Then they called their dogs and quote similar thing. So not email for the key email of course. Finally called their dog and called SOS dogs who Id who are the key you. All right. That's a it. So everything you want to encode is going to take these values on and quote them based on these keys. Which is exactly what you want. Now let's worry about decoding. So for decoding it I want to say this because there has the information that I want. Let's find them out. So first of all don't let name be a decoder not the code object or key. The key is obviously called name. Then you could actually say necessarily make this one as a string so that you know you know it is going to be exactly the type that you want. So it isn't necessarily a good thing. I think I'm a bit of a mistake here. I said decode the bully and what I meant. The code objects. Yes. So the code object or key name. Then there's one other one. Let main be a decoder dot you call ecord object for key. Again I made the same mistake I don't know why that's happening. The code the caught up just for cheap. Exactly. Email is a strain and finally lets you IDB a decoder dot the code object for key. The key is your ID. Make sure your spellings are absolutely identical because if you make a mistake it won't work as a string. I know that we have these now that we have a name and even you Id simply go ahead and say Well so does it read these information we name. The main with you ID so that in it that override that you have written we can't easily use it in here to initialize or object of the type. Use a class that's allowed that so it should work. Now with a little bit of a warning here. Which means some of its Yes. The IT wants us to insert the convenience of course because the actual end is this one. So this is a very similar practice that we have to do for all of our custom classes. I want to spend time in doing good for every class. A grocery list ignores as well as the item class. I'm just going to give you the code for them. Well if you do them yourself. Just because it is under the exact same thing. A coder takes your values on in code them using keys that decoder knows your values and you can read them based on those keys exactly like how a dictionary works. Once you get information you initialize and you copy all your class type. That's all we have to do for now. Let's move on to our next lesson.