Skip to content

Instantly share code, notes, and snippets.

@wildthink
Created May 18, 2018 19:44
Show Gist options
  • Save wildthink/738ba902fec4c7956d5ad5c2da81b1bf to your computer and use it in GitHub Desktop.
Save wildthink/738ba902fec4c7956d5ad5c2da81b1bf to your computer and use it in GitHub Desktop.
import Foundation
// Variation of
// https://gist.github.com/vincent-pradeilles/1886454090d18fc0e3c7cfcfd96ad6bb
protocol WithSet {
associatedtype S = Self
func with<T>(_ property: ReferenceWritableKeyPath<S, T>, setTo value: T) -> S
}
extension WithSet {
func with<T>(_ property: ReferenceWritableKeyPath<Self, T>, setTo value: T) -> Self {
self[keyPath: property] = value
return self
}
}
extension NSObject: WithSet {}
/* Example Usage
let label = UILabel()
.with(\.textColor, setTo: .red)
.with(\.text, setTo: "Foo")
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment