Skip to content

Instantly share code, notes, and snippets.

@kimar kimar/Applicable.swift
Last active Jan 10, 2020

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

dkyowell commented Jan 10, 2020

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
You can’t perform that action at this time.