What is guard? 1. Guard is a new use of judgment statements in Swift 2.0. The GUARD statement is similar to the if statement in that it determines what to do next based on the Boolean value of the expression after the keyword. Unlike if statements, guard statements have only one code block, whereas if statements can have multiple code blocks. (if, else if, else) 2. So what does guard do? As the name implies, guard is used as a guard. When you do not meet my requirements, then please go out; If yes, go to the next step.Copy the code
1. Guard can be used to front the processing event that does not meet the condition, so that the application does not have to miss something during development. 2. Guard also reduces the amount of nesting in conditional statements, making code cleaner and easier to read. In the book Zen and the Art of Objective-C Programming, there is a golden Road:Copy the code

3. What is the use of guard

Let's take the example of checking in at a movie theater: // struct Ticket {var movieName:String// movieName var TimeValid:Bool = true} func checkTicket(Ticket :Ticket? ,currentMovieName:String) { guard let _ = ticket,ticket? .moviename == currentMovieName else {print(" not this movie ") return} guard ticket? .timeValid else {print(" the ticket has expired ") return} //TODO: print(" the movie is ready ")}Copy the code