Last active
January 10, 2020 03:41
-
-
Save kimar/48c7c787db87c63181c3a2c93ddfa88f to your computer and use it in GitHub Desktop.
Implement `apply` on NSObject (and other types)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing. This is my favorite tip from the past year.