Instance Method

appending(path:)

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

Declaration

func appending(path: AnyKeyPath) -> AnyKeyPath?

Parameters

path

The key path to append.

Return Value

A key path from the root of this key path and 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 key paths from Array<Int> to String and from String to Int, and then tries appending each to the other:

let arrayDescription: AnyKeyPath = \Array<Int>.description
let stringLength: AnyKeyPath = \String.count

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

let invalidKeyPath = stringLength.appending(path: arrayDescription)
// invalidKeyPath == nil

The second call to appending(path:) returns nil because the root type of arrayDescription, Array<Int>, does not match the value type of stringLength, Int.