Last active
June 13, 2020 12:36
-
-
Save aainaj/9743e856d0886e0d1218c4a35b458b21 to your computer and use it in GitHub Desktop.
ReferenceKeyPathEditable Protocol
This file contains 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
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