Skip to content

Instantly share code, notes, and snippets.

@lammertw
Created June 17, 2016 15:33
Show Gist options
  • Save lammertw/925a21bc1b670567172881e8addbf45b to your computer and use it in GitHub Desktop.
Save lammertw/925a21bc1b670567172881e8addbf45b to your computer and use it in GitHub Desktop.
Filter objects into Optionals
extension Filter {
/// Allows filtering of non sequence types.
///
/// let label = UILabel().then {
/// $0.textAlignment = .Center
/// $0.textColor = UIColor.blackColor()
/// $0.text = "Hello, World!"
/// }
public func filter(@noescape condition: Self -> Bool) -> Self? {
return condition(self) ? self : nil
}
}
extension NSObject: Filter {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment