Returns a new key path created by appending the given key path to this one.
SDK
- Xcode 9.3+
Framework
- Swift Standard Library
Declaration
func appending<Root>(path: Any Key Path) -> Partial Key Path<Root>?
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: PartialKeyPath<Array<Int>> = \.description
let stringLength: PartialKeyPath<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 array
, Array<Int>
, does not match the value type of string
, Int
.