Key-Value Coding(KVC)

struct Dog {
    var name: String
}

let myDog = Dog(name: "Fido")
print(myDog.name)  // 직접 접근

let nameKeyPath = \\Dog.name
print(myDog[keyPath: nameKeyPath])  // KeyPath를 통한 간접 접근

Key-Path Expression

struct SomeStructure {
    var someValue: Int
}

let s = SomeStructure(someValue: 12)
let pathToProperty = \\SomeStructure.someValue

let value = s[keyPath: pathToProperty]
// value is 12
// Example of KVO
class SomeClass: NSObject {
    @objc dynamic var someProperty: Int

    init(someProperty: Int) {
        self.someProperty = someProperty
    }
}

let c = SomeClass(someProperty: 10)

c.observe(\\.someProperty) { object, change in
    // ...
}
var compoundValue = (a: 1, b: 2)
// Equivalent to compoundValue = (a: 10, b: 20)
compoundValue[keyPath: \\.self] = (a: 10, b: 20)
struct Task {
    var description: String
    var completed: Bool
}

var toDoList = [
    Task(description: "Practice ping-pong.", completed: false),
    Task(description: "Buy a pirate costume.", completed: true),
    Task(description: "Visit Boston in the Fall.", completed: false),
]

// Both approaches below are equivalent.
let descriptions = toDoList.filter(\\.completed).map(\\.description)
let descriptions2 = toDoList.filter { $0.completed }.map { $0.description }