Swift 5 Brand New Result Type: Write Cleaner API code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey what is up everybody welcome back to a brand new video on this YouTube channel here I hope you're having an amazing a morning in today's lesson what I would like to talk about is a brand new variable type that just recently got introduced inside of these Swift v programming language and so if you're not fully aware Xcode 10.2 just finally came out of beta mode so you can download the new Xcode inside of the App Store and start playing around with the new available features inside of Swift 5.0 and so like I was mentioning earlier as well if v comes up with this new result type variable and it's actually very useful and it's going to change the way that you write your Swift API is from now on so the way I'm going to illustrate how and why you want to use this result site is to briefly go over this example of building out this app over here right so we have this list view and we have all the courses available inside of this list such as App Store tinder swipe at match Instagram firebase podcast and so and so forth if you want to watch these courses you can visit the links in the description below but for today's lesson I'm going to show you how we can actually fetch all this data from this URL right over here so if you visit API dot let's build that app comm JSON decodable and courses you'll come upon this array of course objects right so for example instead of here we have ID name link image URL and number of lessons and this video I'm gonna parse all of this object right here all these course objects I'm gonna show you how we used to do it in Swift 4.2 and then I'll briefly introduce why you would want to use this result type and what this new syntax needs to be okay so hopefully that sounds good and let me copy this URL and go back into Xcode and I'll write out the brief code inside of this view controller file right over here you can see I've already spent the time and created our model course object right above and the way that you normally write out this fetch code is pretty simple if you've done it before so let's say fetch of course JSON and this is going to be a function to write below so why don't we make it a file private and fetch course fetch courses JSON and this guy is our function that we need to call right it's a fetch courses JSON and why don't we just print out one two three just for now and I'll run this inside of these lesson simulator just to make sure that we have the one two three being printed AB and so we have one two three so that means it work it's a go now this function right here usually contains a completion block for the first parameter and you can do this a lot of different ways so I've seen some different methods of using a success completion and also a failure completion and the other way that actually prefer it is to use this style right here so let's say the completion block completes with an array of course objects right but you don't know that you always get this list bag so you have to make it optional like that and if something goes wrong during the actual fetch you might get a potential error so this is kind of how you handle that use case and this is actually kind of awkward here and then that's kind of why we would like to use this new result type but before we get to that let me lift JSON parsing code and the way you do this is pretty simple you just want to say let URL string equals the URL or just make this a string and copy and paste the actual URL right here just create your URL so URL might be typing this out a little bit quick but you know this stuff is pretty standard by now and so once you construct our URL you want to make sure you call URL session in this guy as a shared data task with a completion Handler and you just want to pass in your URL right over here and then for the completion handler you can just hit enter like so and you'll get your data you'll get your response and then you'll get your potential air all right so this is pretty standard kit and make sure to always call a dot resume over here and I think that should be okay all right looks pretty good I think this URL right here might look a little bit weird so let's kind of see I think we need a let in front of that guy and all this code should good secure down all right so let's make sure to fix this with the completion here hit enter and you'll get this kid which isn't all that great so let me just leave that for now down inside of here is kind of where you would execute the actual JSON parsing for all these objects here and it's not very pretty simple but before you actually do the decoding and the parsing you have to check this potential error here and maybe the response as well but you have to say if let air equals air and then if this happens you want to call completion with the actual parameters of nil for no courses and then the error object like so and then return so this is pretty basic and hopefully you guys have written this before yeah you want to make sure that this guy is a call types I believe you have to write this over here so air arrow and like that with Brent Brent this way you can get your closure correct so let me make sure to type out this guy correctly one more time so hit backspace completion entered in your courses and you get your potential error like that all right so again pretty standard stuff you want to make sure this has an escaping on this left side here and you should be alright finally if this is all successful you want to write some code that kind of looks like this so what of course is equals I guess it's jason decoder and decode and let's see course that's self maybe I don't want the array version instead like that and then you want to use the data that's up over here and let me just use data bang just to be a little bit simpler here and then finally you need to actually try this right here so if you click on that it tells you that you need to try the actual decoding say let me just do this really quickly and then I'll introduce the actual result type so cut that put that in here catch the potential here like so so let's just say catch and we'll call completion again if there's an error well you know past this potential error so this is the actual JSON here you can let this guy be a JSON error instead maybe this is a little bit clearer pass that in here and then finally if everything happens correctly you want to complete with the actual courses from the above and the nil error object like so all right so I'll play this code is looking pretty good I believe you need to try that there and everything looks to compile correctly last thing I'll do is for the view controller the completion block here you just want to say something like if let air equals the potential air it will say printout failed to fetch so fetch courses and let's just say air like Saif Ali so I mean and then down here you can say something like courses that for each inter intercourse and let's just print out course dot name and I'll run this guy right now I believe everything looks ok the decoding looks fine and down here you'll see that we're printing out all the names of the courses from our JSON objects here right so after just an API name and name Instagram so all that's just being printed down below and this looks kind of ok and for the longest time if you've been using this with 4 points you this is typically the code that you weren't right but the weird thing about this code is that when you're fetching the actual courses and when you're inside of this completion block here it's kind of ambiguous what's going on right so ambiguous situation here I think there's a actually you and the big was but make sure to correct me if I'm wrong basically you don't know if you have an error and you don't know if you have courses and you're kind of forced to make sure that you check it here and so due to the ambiguity of this situation inside us with 5.0 is the brand-new result side and the weight you use is actually a little bit tricky so let me show you how to type out this syntax here so the way that you introduce the result time is instead of using the closure that looks kind of like this here with the course and air type maybe I'll remove the left side so instead of using this here what you want to do is to complete on a result type like that so it's a value that represents either a success or a failure include it including an Associated value in each case so it might be a little bit confusing what that means but basically you want to use a result type that has two generics inside of it so the generic is the actual course array that looks like this here instead of course and then the second generic is just a generic error like that and you can try to run this now I believe it should be okay so with the modification of the completion block inside of here what you want to do is you want to complete and this guy only expects one result time variable now and so this is actually just one variable and the way that you do this is in the use case of the air you want to call it dot failure with the actual air like that so you don't really need to make this a weird call with two parameters anymore and just make sure to call failure and down here you don't want to call these two parameters either so you can say completion and this is the success case dot success and call it with the courses from above so let me comment that out and inside of here you would do something like what we did before with the failure of the json air just like so let me comment that out and you can now try to run the code again however i problem now is that this completion is also a little bit broken so instead of making this call here maybe I'll just comment out all these lines you can call fetch courses with the JSON hit enter here you'll get this result and what you do with this result is you're actually forced to check the air and the way this is going to work is you want to use a switch case so a switch case looks kind of like this over here so switch let's get the autocomplete to help us out here now let's take the switch case here paste that in here and the way you actually make this call is to use the switch on the res or the result and then just do that the pattern is going to look something it's a success and the way that associated values work with these result type enumerations is you can just say let right here in courses like so and this will give you a handle on all the courses that are coming back from the fetch call so an intercourse like that and it's very similar to printout of course that name does pretty much what we had earlier right here but the only difference is that the syntax is much easier to read and in addition to these success you want to catch the failure as well so let here just like that : and what does they put out you know failed to fetch courses and just print out the air on the right side you don't need this weird return statement here everything looks good and I believe since we're catching the success and failure we don't really need a default case for our switch statement and so I'll try to run this again let's see if fetch is used before I think I'm missing a brace right here so let's close the switch statement off and run the code one more time you'll see that all the courses are going to be printed out down below just like what we had before and again the reason why you and want to use this result type is you no longer have to deal with this weird situation where you have a completion handler that has two parameters that are potentially nil inside of this index here is very explicit what's going on right you have your result in the case of success you do something in the case of failure you do something else so having this result type is a lot more useful in terms of writing cleaner api's the only problem is you have to get used to how to get the actual values outside of your results type using this bit of code here but aside from that I think it's a pretty welcomed addition inside these let's programming language in my opinion all right everybody that's gonna wrap it up for today's lesson and hope you found it helpful if you wanna download the source code for all these things that you saw in today's lesson you can find a link in the description below if you want to learn more about how to develop more modern applications instead of Iowa using these Swiss programming language make sure to check out the courses available down in the description as well that's gonna be it for today I'll see you in the next video bye guys
Info
Channel: Lets Build That App
Views: 52,327
Rating: undefined out of 5
Keywords: ios, swift, development, tutorial, learn, xcode, programming, code
Id: 9iazQQdNoNU
Channel Id: undefined
Length: 13min 9sec (789 seconds)
Published: Tue Apr 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.