In common

Both are keywords used for process control, and both can do one thing if the condition is met, and do another thing if the condition is not met

grammar

Simple use of if

if ! Name. IsEmpty {doSomething()} else {show(" name isEmpty ")}Copy the code

Simple use of GAurd

guard ! Name. IsEmpty else {show(" name isEmpty ") return} doSomething()Copy the code
  • Syntactically, if code that meets the criteria is placed in the preceding closure, and else code that does not meet the criteria is placed in the else closure. Code to be executed by guard that does not meet the criteria is placed in the else closure. Code that does meet the criteria is placed after the Guard statement, not in any of the Guard closures
  • An important feature of GUARD is that an else closure must be followed by a keyword or method executed after the interrupt, such as return, continue, break, fatalError, throw, etc. There is a big mistake in thinking that only return can be used in else closures

When is guard appropriate

1, multiple optional unpack, and need to meet the conditions, such as when doing login function

There are two ways to use if: method 1:

if let username = usernameTextField.text, ! username.isEmpty { if let password = passwordTextField.text, ! IsEmpty {register(username, password)} else {show(" username cannot be empty ")} else {show(" username cannot be empty ")}Copy the code

Method 2:

if let username = usernameTextField.text, ! username.isEmpty, let password = passwordTextField.text, ! Password.isempty {register(username, password)} else {show(" username and password cannot be empty ")}Copy the code

Disadvantages of these two methods: Method 1 has multiple nesting, if there are more than username and password, the nesting is deeper; In mode 2, the statement after if is too long to be easy to read and understand, and the structure is not clear

How to use Guard:

guard let username = usernameTextField.text, ! Username. IsEmpty else {show(" username cannot be empty ") return} Guard let password = passwordTextField. Text,! IsEmpty else {show(" password cannot be empty ") return} register(username, password)Copy the code

With GUARD, the structure is clear, easy to read and understand, and does not create nesting

2. Interrupt the current flow. If conditions are not met, there is no need to perform the following logic, such as checking whether the mobile phone number is correct

Use the if

Func verifyMobilePhoneNumber(_ phoneNumber: String) -> Bool {if phoneNumber. IsEmpty {return false} /// check logic... }Copy the code

The use of guard

func verifyMobilePhoneNumber(_ phoneNumber: String) -> Bool { guard ! Phonenumber. isEmpty {return false} /// check the logic... }Copy the code

Both if and GUARD can meet the functions, but the advantage of Guard is that when we see the conditions in Guard, we know that if the conditions are not met, we need to interrupt the current process. When we use IF, we need to look at the contents of the closure corresponding to if to determine whether to interrupt the process. The example here is simple, and we don’t see too many advantages. This is where guard’s advantage comes in

When do you use if instead of guard

When meet the conditions to do something, and then continue to do the things behind, such as cleaning, zhang SAN sweeping the floor, and then mop the floor, Li Si sassafras the blackboard, and then mop the floor, other students mop the floor

func cleanClassroom(name: String) {if name == "sweepTheFloor()} if name ==" cleanTheBlackboard()} mopTheFloor()}Copy the code

Obviously, you can only use if here, not guard

conclusion

The advantage of Guard is that else closures must use statements that interrupt the current flow, and the code that meets these conditions does not need to be in any of guard’s closures. Using Guard solves some of the problems of if nesting, and some of the problems of if code that is unreadable and unstructured