<div dir="ltr"><span style="font-size:13px">&gt; What is &quot;guard&quot;? why its the opposite to &quot;if&quot;!</span><br><div><span style="font-size:13px"><br></span></div><div>Not exactly. It’s a guard clause, designed to be used at the beginning of a method to ensure that certain conditions exist, and to exit early if they do not.</div><div><br></div><div>In addition, guard in Swift works different to if in that any `let` declarations made within the guard clause are set within the scope of the parent method – especially useful if you need to unwrap optionals. With if, they are only valid within the if block.</div><div><br></div><div>For example:</div><div><br></div><div>    if let name = optionalName as? Name {</div><div>      print(name) // okay</div><div>    }</div><div><br></div><div>    print(name) // out of scope</div><div><br></div><div>But with guard:</div><div><br></div><div>func printName(optionalName: Name?) {</div><div>  guard let name = optionalName else { return }</div><div><br></div><div>  print(name) // this is okay</div><div>}</div><div><br></div><div>So yes, the distinction is subtle – and in Ruby (the language I use in my day to day work) we implement guard clauses with ‘if&#39; or ‘unless’ blocks or modifiers.</div><div><br></div><div>But with the difference in scope of assigned variables between `if` and `guard` in Swift, I think your suggestion would make things slightly more confusing, instead of slightly less.</div><div><br></div></div>