<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/AnyKeyPath/appending(path:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s14_AppendKeyPathPss03AnybC0CRszrlE9appending4pathADSgAD_tF::SYNTHESIZED::s:s10AnyKeyPathC"
  },
  "title" : "appending(path:)"
}
-->

# appending(path:)

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

```
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`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)