Skip to content

Instantly share code, notes, and snippets.

@kimar
Last active January 10, 2020 03:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimar/48c7c787db87c63181c3a2c93ddfa88f to your computer and use it in GitHub Desktop.
Save kimar/48c7c787db87c63181c3a2c93ddfa88f to your computer and use it in GitHub Desktop.
Implement `apply` on NSObject (and other types)
import Foundation
protocol Applicable {}
extension NSObject: Applicable {}
extension Applicable {
@discardableResult
func apply(_ closure: (Self) -> Void) -> Self {
closure(self)
return self
}
}
extension Array where Element: NSObject {
@discardableResult
func apply(_ closure: (Element) -> Void) -> [Element] {
forEach { closure($0) }
return self
}
}
@dkyowell
Copy link

Thanks for sharing. This is my favorite tip from the past year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment