#95: Key Paths in Swift

A key path is a typed reference to a property.

struct User {
    let name: String
    let age: Int
}

let path = \User.name
let value = User(name: "Ada", age: 32)[keyPath: path]

This is useful when building generic sorting, mapping or binding code around properties.

Once you start seeing them as “property values you can pass around”, many Swift APIs make a lot more sense.


I'm also on Twitter and GitHub.

Subscribe via RSS.