compactMap and flatMap: Clean Up Optionals and Crashes!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey what is going on everybody welcome back to this awesome fantastic YouTube channel today hope you're doing well now happy to ran into this function called a flat map or what it's called now it's compact map inside of the Swift programming language I know every time I run into myself I always find it pretty confusing and pretty awkward as to how to apply it inside of my logic of my programs so what I would like to do in today's video is to illustrate a couple of different examples as to how to use compact map correctly and hopefully I can shed some light as to you know how useful this function is instead of our iOS apps alright so having said all that let me draw your attention to the Swift playground on the left side of the screen here and now how exactly do we use a compact map is a question we want to answer in today's video right and well before I can actually show you how to use compact map let me kind of give you a couple of different structs and kind of set up the foundation for our program here so the main thing that I would like to figure out is how we can calculate some kind of total value based on a couple of different objects instead of our program and so the objects that we are going to have are some store objects right so this guy is going to be a struct and it's going to be a store just like this and a couple of different properties that exist on a store I'm going to just make it up right here I wanted to call this an electronic so I like chronic hard trying to Hardware spell this guy correctly and this is just going to be a simple array of string objects like that and then perhaps we can also give this thing a name as well so a very simple struct type of class and at the very bottom what I'd like to figure out is you know fine all of the electronic electronic items souls items sold in the area and so that's the question I would like to kind of figure out and the way I'm gonna set this up is to create a couple of different store objects that let's call this guy target you'll find a lot of targets out here instead of California on the west coast and I'm going to call this guy a store object create it using a name so obviously we are going to call this tarjay or Target on the right side we are going to give a couple of different electronic hardware that you know the store sells so I think every time I walk inside of the target electronics section I see some iPhones and also some iPads and let's spell this guy I pad and you know maybe it's a flat-screen so I think this is one word flat-screen TVs and you know basically this is how a Target store is set up next that we are going to declare another one of my you know maybe not so favorite stores out here let's call this Best Buy's and very similarly like Target we are going to construct a name of the Best Buy and some people call it worse buys electronic hardware what do they sell over here I think they kind of sell very similar items so you know laptops and big fridges or fridges like that you know whatever you want to add instead of here is okay and then lastly we have a couple of different stores now outside in the Dublin area which is where I'm currently living we have some stores like Bed and a bit and bath and Bed Bath and Beyond I think their call it's a Bed Bath & Beyond like so so this store is a little bit trickier because they don't exactly sell a lot of electronic hardware so we can do is for the second property you can just declare an empty array like that it doesn't exactly make sense to declare it using a blank array but you know that's what we'll do for now because our struct right here isn't able to support anything else so that's a little dude and this guy will call Bed Bath & Beyond so I'll just use the ampersand there okay so here we go we have these three stores I kind of set up like so Target Best Buy's and Bed Bath Beyond so next thing I like to do is to figure out all of the different electronic items that are sold inside of these three stores and you know like any other programming language there's a lot of different ways to do this the quickest and dirtiest way is to do this right here so let the items Eagles target electronic hardware plus Best Buy not electronic hardware like that so do the same thing for Bed Bath & Beyond so dot electronic hardware you know it's very simple access to property on the store object itself and inside of playgrounds you can just hit the play button on the left side of the screen and eventually it'll calculate everything on the right side you can click on this list here which is very hard to read so click on this guy instead which is also not perfect but you can kind of see all of the electronic items are now combined instead of this are right here I don't exactly like seeing this I'm just going to just print items on the very bottom and run about one final time you'll see iPhone iPad flat screen TVs laptops and big fridges on the very bottom okay so this is a very basic way of calculating these items right here the problem with using this approach is that every time you add a new store you have to give something like yeah you know add this thing on the very end use stored electronic hardware so this is very problematic and you can see yourself doing this a lot of different times right so you want to add a different one and call this let me like new store too so this is not the best approach of actually handling this little bit of an algorithmic logic and the way to actually fix this is to use some kind of array so what I mean is you want to say something like this so I'm gonna call this item is 2 and the way to cap away items 2 is to use Target and we will use best buy the best buys and Bed Bath and Beyond so now all this stuff is kind of stored inside of an array and what you can do is you can use a map function right here and to make this a little bit more concise is what you would call it you just return 0 dot electronic hardware like so and now I'm going to product items to down here and finally we'll run this guide one final time inside of our function here and as you can see the array that you're getting back is a multi-dimensional array and so the first one starts here and then the second one starts here and then we have the third empty array at the very end there so not exactly perfect and what you can do instead is you can use a flat map to you know flatten all this stuff inside of here and please forget how to apply this logic correctly so let's change this guy to flat map like that and keep the right side very similar I'm going to run this guy one more time and see whether or not it works and it looks like it does we have iPhone iPad flat screen TVs laptops and big fridges so everything up to this point the program looks you know it looks relatively okay the next problem that you'll encounter is for a lot of these stores right here such as Bed Bath & Beyond I was mentioning earlier that using an empty array to store electronic hardware for this particular store doesn't exactly make any sense because this store has really no reason to sell electronic hardware and so if we're stores like Bed Bath and Beyond or maybe some kind of pizza shop this guy right here you might want to declare it as an optional array instead so I believe this is going to be okay and once you make this guy optional you know you might want to make this a bar instead so that looks a little bit better to me and now this guy is an optional right here so this really doesn't work out correctly anymore for the items right here you can't really add these electronic hardware arrays like that you have to fix it with a bang a bang and a bang like so so the items right here is still okay and like I was mentioning in other videos you never want to use as bangs and I'm just remove that and use items two right here instead and as you can see once you modify this guy to an optional on the very right side it'll kind of tell you this little compact map warning right here so it tells you that flat map has been deprecated and you want to use compact map instead so the fix here it modifies this to compact map instead all right so now with that a little bit of a change let's kind of see what items two will look like when I run this print statement and one more time and you can see for whatever reason I can't figure exactly out why the compact map stores all of the items inside of this multi-dimensional array again but the good thing about this approach is that you don't have to handle this optional value what this guy is going to do is if all of these values inside closure right here if this guy is an optional what I'll do is you know just toss it out and return you an empty array like that and the last thing you want to do is do flan this guy one more time if you want to take all these items and put it into one single array so the hack approach I would say is to use flat map against a flat map and you can just use a brace right here in dollar sign zero and brace again and this should come out correctly I'm gonna run this one final time you'll see that the bottom is going to have just a flattened array and it kind of looks like what we had before iPhone iPad flat screen TVs and so and so forth so the whole thing about compact map is that sometimes you have objects inside of your iOS programs that contain a nil or an optional variable such as electronic hardware right so this is just a very basic simple example and whenever you have a class structure that involves an optional you don't exactly want to unwrap err optionals with bangs bangs and bangs like this example here what's it make sure that the logic is a little bit cleaner and the way to make it clean is to use this compact map the notion of comment map is to make sure that it handles the optionals for you and the tricky part about this is that you want to plan the actual and resulting array one more time with another flat map call now finally with all of the logic of our application and mapped out correctly instead of our playgrounds area you know no pun intended one last thing I would like to do is to drive this example home by making sure that we can declare this variable right here for electronic hardware I can actually declare this as nil now and what I'll do is I'll comment out this code and try to run this again you'll see a crash due to the third parameter right here so if you click on this guy maybe you'll get a error that makes sense but basically this character electronic hardware is an optional nil and whatever you use a bang on this guy you'll see that you'll get a crash like this and so what you can see is that the compact map approach is a lot better because you can run this and you'll see that everything works fine you don't get a crash all you'll get is your beautiful string array down below all righty everybody that's gonna wrap it up for today's video hopefully enjoyed it and hopefully you found something useful that you can also apply instead of your own iOS applications if you have any questions or comments make sure to leave those down below in the comment section obviously if you want more videos like this make sure to also subscribe to the channel if you want to learn more about modern iOS application development make sure to check out the a couple of courses using the links in the description below that's gonna be it for today's video hopefully I'll see you in the next video I guess
Info
Channel: Lets Build That App
Views: 8,154
Rating: undefined out of 5
Keywords: ios, swift, development, tutorial, learn, xcode, programming, code
Id: EUtTU4lW2QI
Channel Id: undefined
Length: 11min 27sec (687 seconds)
Published: Sat Apr 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.