Transcript for:
Swift for Beginners: Guard Statements

welcome back to another lesson in Swift for beginners in today's lesson we will be covering the guard statement so once again we're back in our playground we've gotten rid of everything from the last lesson that we have our title appears so let's jump right into things so a guard statement is fairly common in more moreso newer languages and what people are to call functional languages and it's very similar to an if statement that we saw in our if-else conditions video so let's just start off by writing a function and let's call it string equals let's call it number larger than 5 let's say number larger than 5 let's go with this it'll take a parameter of a number it'll be an int this function will return a bool let's just say it returns true so a GART statement so before we've been talking about the guard and theory what you would do in here is you would say if number is less than 5 let's return false right always make sense and this is complaining because hopefully that's good yeah so basically if the number is less than 5 we're gonna say false this number is not larger than 5 otherwise it won't come into here and we'll go true so how would we write this as a guard statement so all we would do is change this to guard and this would be an else so it's very similar to an if-else but what you're basically saying in this statement is very similar it's just worded a little differently so in layman's terms it you're what you're saying is you're guarding you want to make sure that number is greater than five right so it's the inverse so if the number is greater than five you can continue down here you're guarding against this case but if the number is not greater than five go into this else right so in other words if the number is less than five you're gonna come into this else and we're going to return false and that's gonna be the end of our function because you know we returned already here we can't go further down the code execution in our function has stopped so you can see the similarity between the two statements but you can also see that the guard if you throw it onto one line like this is a little cleaner in terms of visualizing it and readability sake but that's all the guard really is it is a little fancier way to do it if else condition the other big benefit to a guard similar to an if condition is you can comma separate in here multiple things that you're guarding so you can say if number is greater than five and number is greater than 4 which realistically does it make sense to do this in an actual app because if it was greater than five it would be greater than four as well but for demonstration purposes you can chain on multiple things you in a guard so for example let's say you are building a login form and the person needs to type in a username and a password in an iOS app when they press sign in you want to make sure that there is text in both of those fields before you try to sign the person in so you might do something like guard make sure this field has text and guard make sure the password field s text if they don't you can maybe throw it like a pop-up and show the user an error message and say hey you need to enter in something so we can sign you in so that's what a guard statement essentially is now what I want to show you is the other very common thing that a guard statement is used for and that's for unwrapping optionals so if you're not familiar with optionals or unwrapping them I encourage you to go back to an older lesson and take a look at that as it's super important but what we're gonna do in this case is we're gonna say let Tex which is a string optional equal mil and down here we're gonna say with this virus we can reassign it we're gonna say text at this point is gonna equal hello world we actually don't even need this because this is nil by default and we can get rid of this function but basically we've created a variable it's text the type is string optional and we're setting it to hello world down here so now if we want to use this Texas actually get access to it in a prior video we've seen that we need to unwrap it and we would do that by doing something like if value equals text then if we print value well see down here that we get our text because something optional is again kind of like a box and you need to check that there is something inside of set box so you can get at the actual value so you can as you can kind of imagine do this through a gaurd statement as well so we're gonna do guard let value equals text else return and then down here we can say plan value and this might yell at us because we're doing a return outside of a function which it is so let's put this in a function let's put this right here hopefully that should solve it which it does and then let's get rid of this print so we can call this function and make sure that's working so let's call print value and at this point which we get hello world down here which we do so now imagine if we didn't set the text here so if we go and comment out this line we won't get a print here because it's gonna go into this function and it's gonna say hey I'm gonna guard and make sure there's a value inside of this text and if there is we can keep going we're really good to go but if there's not just return out of this function and don't continue on so it's a little more readable in my personal opinion you're guarding something is there is correct and your criterias is fulfilled and it's less verbose than an if statement so using guards to unwrapped optionals and using guards as a simpler way to replace if-else conditions is wildly popular and swift and it's something that I strongly suggest that you be comfortable with you might not have seen it in older languages older programming languages if you're if you have any background in older programming languages but that's essentially what a guard is and I think that's where I will leave it for this lesson I hope I was able to explain guard statements well to you if you like this video please do leave a like follow subscribe share it helps allow quite a bit don't hesitate to leave comments in terms of clarity you're asking for help or questions more than happy to help and I will see you guys in the next lesson thanks for watching [Music]