Skip to content

Instantly share code, notes, and snippets.

@dineshba
Last active May 19, 2018 18:25
Show Gist options
  • Save dineshba/9e791e1e117819983b7ddc0b04c4b3c0 to your computer and use it in GitHub Desktop.
Save dineshba/9e791e1e117819983b7ddc0b04c4b3c0 to your computer and use it in GitHub Desktop.
func printSummary(_ programmer: Programmer) {
guard let laptop = programmer.laptop else { // set the value of laptop if `programmer.laptop` is not nil
print("\(programmer.name) have no laptop") // and will go line 6. If `programmer.laptop` is nil, it will
return // enter into else case and return to caller of this function.
}
guard let laptopFan = laptop.fan else { // Syntax
print("\(laptop.name) contains no fan") // guard let constant = expression else {
return // return returnValue
} // }
print("\(laptop.name) contains \(laptopFan.name) fan")
}
printSummary(programmer) //guard let should be used with some function. Because it is based on early exit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment