8. Encoding data - Swift

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my name is Stewart Lynch and this is the eighth of a nine part series on understanding how to parse JSON using the codable protocol in Swift and swift you why this video will learn all about encoding our data will learn about the incorpo protocol and the codable type alias will work through many of the examples that we used for decoding make sure that those structure classes are also incredible so that we can encode our application data into a JSON object if this is something you want to learn then keep on watching we've been spending a lot of time exploring decodable because that is what you do most of the time but what if you want to reverse the process and encode your data into a JSON object or string this is actually pretty straightforward so we'll look at some of our earlier examples and see what that entails consider this first example have a person struct that is currently decodable and i've also created an array of 2-person and placed it into a variable called people what I'd like to do is encode this array as JSON it's actually the reverse of decodable the first thing we need to do is to make our struck conform to the in colorable protocol it's already decodable and it turns out that all we need to do is change decodable to encode a ball and we get no errors the problem is we've now lost our ability to decode well the good news is that you can use decodable and encode a ball but we can do even better we can use the type alias codable if all you're going to do is decode data you can just use decodable if you just want to encode use encode ball but you'll find that most examples you'll see use the type alias codable and that means you're covered for both there will be something else that we have to do to make some of our strux conform to codable but we'll come to that in a minute so what do we have to do now just like we defined a JSON decoder we can define a JSON encoder and just like decoder JSON encoder can throw an error so we'll need to use a try I know this is okay so we'll use try with the exclamation mark well now that we have people JSON data it's a data object and if we want to see what the text of this JSON looks like we can convert it to a string by using the utf-8 encoding on this data object you should be able to as a JSON string and in fact if we paste this into our JSON for matter and process it we see that it is valid let's comment this print statement out and move on to the next example this was our second example this is the family struct that has two properties family name which is a string and members which is an array of person have created an instance of this family with two members and assigned it to the constant my family what I want to do now is encode this my family as JSON first of all let's make the family strong codable and when we do that we get an error because in order for a struct to be codable all of the enums in structs within it also have to be codable so we can change gender and person to be codable as well so we can just repeat the process of the last example we can define a second encoder although we don't have to we will and then we can try to encode it and then convert the data to ace so that we can print it out you we will view this data again in our J's for matter and take a look at it and we can see that yes it is a valid JSON struct again let me just comment this out before I go on to the next example you can encode structs and classes with dates as long as you provide your encoder with a date and coding strategy for example consider this we have our event struct that contains a string a date and a URL and I've created an array of these to give me two events and I've assigned it to the constant my events what I want to do now is to encode this array as JSON first let's change decodable to codable and create a new encoder so we can assign a date encoding strategy this is similar to assigning a date decoding strategy let's create a new encoder we'll specify the date encoding strategy number of milliseconds since 1970 we'll try to encode our data and then we all print out the of that data and there you have it we will view this data again in our JSON format and take a look at it and we can see that yes it is a valid JSON struct I hope you can see how easy this is one last example I want to go over is one where we have coding keys for the most part when you're encoding your own data you shouldn't have to deal with coding keys since you define your data and the JSON will be fine however there are two cases that you might have to use coding keys and that's when you either have to provide a JSON file where the key properties for the JSON are different from your own or you have an observable object in a published property wrapper where you're forced to use chording keys when you made that class let's look at this example here I've changed it up a little bit from the example that we had previously had and I've added a published property wrapper on the name and also added a mapping of the JSON age in years to a more simpler age variable that required us to have our initializer and our coding keys as soon as I change this to codable I get an error indicating that B type user does not conform to the protocol and codable what I have to do is effectively reverse the custom initializer and we can do that by using the encode - encoder function the error goes away but that doesn't mean we have a successful encoding let's start the same way that we did with the initializer and define a container but for the encoder using our coding keys and then we can encode each for two variables mapping the coding keys enum to the variable instance our function throws we can just use try here without the exclamation mark and let the error if any bubble up to the encoder itself and handle the error there if we want so we can go try container dot encode and here you see that we can encode all of these different types so we can encode name four key name and age for key age in years you cestus out have already created an initializer for this class so we'll create a new user Terry Fox who has an age of 25 will create a new encoder and will encoder object and then print out the string represent again great it works well now you know how to decode and encode JSON data so that you can load data from a file or API and encode it again back to JSON in the final video of this series we'll look at an app that loads seed data initially from the bundle but when the data is updated right sit back as JSON to the application Documents folder so that upon next launch the data is loaded from that Documents folder rather than the application bundle I hope you've enjoyed this video and learned something if you did please give it a thumbs up and subscribe to my channel that will encourage me to keep on creating more like this in an effort to help new and existing iOS developers hone their skills and move on to the next level I am most active on Twitter so be sure to follow me there and get all the latest news of what I'm up to
Info
Channel: Stewart Lynch
Views: 1,209
Rating: undefined out of 5
Keywords: Swift, Xcode, JSON, Codable
Id: Nh_XZ-F9Br0
Channel Id: undefined
Length: 10min 14sec (614 seconds)
Published: Sun Feb 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.