Generic Instance Method

appending(path:)

Returns a new key path created by appending the given key path to this one.

Declaration

func appending<Root, AppendedRoot, AppendedValue>(path: KeyPath<AppendedRoot, AppendedValue>) -> KeyPath<Root, AppendedValue>?

Parameters

path

The key path to append.

Return Value

A key path from the root of this key path to the value type of path, if path can be appended. If path can’t be appended, returns nil.

Discussion

Use this method to extend this key path to the value type of another key path. Appending the key path passed as path is successful only if the root type for path matches this key path’s value type. This example creates a key path from Array<Int> to String, and then tries appending compatible and incompatible key paths:

let arrayDescription: PartialKeyPath<Array<Int>> = \.description

// Creates a key path from `Array<Int>` to `Int`
let arrayDescriptionLength = arrayDescription.appending(path: \String.count)

let invalidKeyPath = arrayDescription.appending(path: \Double.isZero)
// invalidKeyPath == nil

The second call to appending(path:) returns nil because the root type of the path parameter, Double, does not match the value type of arrayDescription, String.