diff --git a/Sources/HelperKit/Protocols/Apply.swift b/Sources/HelperKit/Protocols/Apply.swift new file mode 100644 index 0000000..c960d25 --- /dev/null +++ b/Sources/HelperKit/Protocols/Apply.swift @@ -0,0 +1,18 @@ +import Foundation + +public protocol Apply {} + +public extension Apply where Self: AnyObject { + /// Calls the closure with `self` as its argument and returns `self`. + /// + /// let button = UIButton(type: .system).apply { + /// $0.setTitleColor(.black, for: .normal) + /// $0.setTitleColor(.gray, for: .disabled) + /// } + @inlinable func apply(closure: (Self) -> Void) -> Self { + closure(self) + return self + } +} + +extension NSObject: Apply {}