Last active
April 24, 2020 00:35
-
-
Save LucianoPAlmeida/5e91b5c413295d2f9f59a82f8de8652d to your computer and use it in GitHub Desktop.
KeyPath<Root, Value> example
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
struct Player: CustomDebugStringConvertible { | |
var number: Int | |
var name: String | |
var debugDescription: String { "Player {number=\(number), name= \(name)}" } | |
} | |
func keyPaths<R, T>(_ base: R, kp: KeyPath<R, T>) { | |
let value = base[keyPath: kp] | |
print("kp value: \(value)") | |
} | |
var kobe = Player(number: 8, name: "Bryant") | |
keyPaths(kobe, kp: \.number) // kp value: 24 | |
keyPaths(kobe, kp: \.name) // kp value: Bryant | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment