Skip to content

Instantly share code, notes, and snippets.

@gmertk
Forked from ecerney/gist:d6bfed963eede7c685a0
Last active August 29, 2015 14:22
Show Gist options
  • Save gmertk/f61c4a7606d9d9c57dba to your computer and use it in GitHub Desktop.
Save gmertk/f61c4a7606d9d9c57dba to your computer and use it in GitHub Desktop.
var x: Int? = 0
// Lets me unwrap x and also check a condition on it
func fooGuard() {
guard let x = x where x > 0 else {
return
}
// Do stuff with x
x.value
}
// I have to check it exists and force unwrap to check the condition.
// I also need to check the bad case rather than checking that its the good case.
// `x! <= 0` rather than `x! >= 0`
func fooManualCheck() {
if x == nil || x! <= 0 {
return
}
// Do stuff with x
x!.value
}
// I am not doing early exiting here, but rather the bulk of code is done in the binding
func fooBinding() {
if let x = x where x > 0 {
// Do stuff with x
x.value
}
return
}
let y = 23
func fooNonOptional() {
guard y > 0 else {
return
}
//Do whatever you want with y since its valid
}
func fooNonOptional2() {
if y <= 0 {
return
}
//Do whatever you want with y since its valid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment