Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@armcknight
Last active May 19, 2017 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armcknight/73f54da770d415545e38e30961a49488 to your computer and use it in GitHub Desktop.
Save armcknight/73f54da770d415545e38e30961a49488 to your computer and use it in GitHub Desktop.
Using an optional return type and returning nil to signify a failure.
class AnObject {
func resultOfWork() -> Any? {/* ... */}
}
func doSomeWork() -> Any? {
guard let result = anObjectInstance.resultOfWork() else {
// log and/or show error alert here?
return nil
}
// ...
return result
}
if let result = doSomeWork() {
// work with result
} else {
// or log and/or show error alert here?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment