Thursday, January 18, 2018

What is a guard statement in Swift?


  • Guard statements are a nice little control flow statement that can be seen as a great addition if you're into a defensive programming style.
  • Guard basically evaluates a boolean condition and proceeds with program execution if the evaluation is true.
  • A guard statement always has an else clause, and it must exit the code block if it reaches there.


guard let courses = student.courses! else {
    return
}