Last active
December 12, 2016 07:29
-
-
Save rmnblm/eeee39dc99fcb8a60ca756ad56c83b06 to your computer and use it in GitHub Desktop.
Swift: if vs. guard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declare three optional variables | |
var firstName: String? | |
var lastName: String? | |
var age: Int? | |
// EXAMPLE: if-Statement | |
if let firstName = firstName { | |
if let lastName = lastName { | |
if let age = age, age >= 18 { | |
doSomething(firstName, lastName, age) | |
} | |
} | |
} | |
// EXAMPLE: guard-Statement | |
guard | |
let firstName = firstName, | |
let lastName = lastName, | |
let age = age, age >= 18 | |
else { | |
return | |
} | |
doSomething(firstName, lastName, age) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment