Skip to content

Instantly share code, notes, and snippets.

@d-date
Created September 3, 2018 20:49
Show Gist options
  • Save d-date/624ad2f7f40557c3bb39bbc6bcfd2462 to your computer and use it in GitHub Desktop.
Save d-date/624ad2f7f40557c3bb39bbc6bcfd2462 to your computer and use it in GitHub Desktop.
How to subscript value using KeyPath #CodePiece #tryswiftnyc
struct User {
var name: String
var location: String
var age: Int
}
var user = User(name: "Blob", location: "NYC", age: 42)
\User.name
// WritableKeyPath<User, String>
user[keyPath: \User.name]
user[keyPath: \User.name] = "Blob 2"
user.name
user.name = "Blob"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment