Skip to content

Instantly share code, notes, and snippets.

/Optional+where Secret

Created January 26, 2017 22:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0c97a1547c90c3dda5ad947e516d23b9 to your computer and use it in GitHub Desktop.
Save anonymous/0c97a1547c90c3dda5ad947e516d23b9 to your computer and use it in GitHub Desktop.
extension Optional {
public init(_ value: Wrapped, where predicate: (Wrapped) -> Bool) {
self = predicate(value) ? value : nil
}
}
func getUserInput() -> String {
return ""
}
Optional(getUserInput(), where: { !$0.isEmpty }) ?? "no value"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment