Skip to content

Instantly share code, notes, and snippets.

@aainaj
Last active June 13, 2020 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aainaj/9743e856d0886e0d1218c4a35b458b21 to your computer and use it in GitHub Desktop.
Save aainaj/9743e856d0886e0d1218c4a35b458b21 to your computer and use it in GitHub Desktop.
ReferenceKeyPathEditable Protocol
enum KeyPathError: Error {
case unableToCast(String)
}
protocol ReferenceKeyPathEditable {
associatedtype Root
func update<KeyPathType>(type: Root, path: PartialKeyPath<Root>, to value: KeyPathType) throws
}
extension ReferenceKeyPathEditable {
func update<KeyPathType>(type: Root, path: PartialKeyPath<Root>, to value: KeyPathType) throws {
guard let writableKeyPath = path as? ReferenceWritableKeyPath<Root, KeyPathType> else {
throw KeyPathError.unableToCast("because of passed \(value) value data type")
}
type[keyPath: writableKeyPath] = value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment