Created
May 18, 2018 19:44
-
-
Save wildthink/738ba902fec4c7956d5ad5c2da81b1bf to your computer and use it in GitHub Desktop.
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 | |
// 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