Generic Instance Method 総称体インスタンスメソッド

appending(path:)

Returns a new key path created by appending the given key path to this one. 与えられたキーパスをこのものに加えることによって作成された新しいキーパスを返します。

Declaration 宣言

func appending<Root>(path: AnyKeyPath) -> PartialKeyPath<Root>? where Self == PartialKeyPath<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. このキーパスのルートからのキーパスとpathの値型、もしpathが追加されるならば。pathが追加できないならば、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: このメソッドを使うことで、このキーパスを別のキーパスの値型にまで広げてください。pathとして渡されるキーパスの追加は、pathのルート型がこのキーパスの持つ値型と合致する場合にのみ成功します。この例は、Array<Int>からStringまでの、そしてStringからIntまでのキーパスを作成して、それからそれぞれを他とくっつけることを試みます:


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 arrayDescription, Array<Int>, does not match the value type of stringLength, Int. 2番目のappending(path:)への呼び出しはnilを返します、なぜならarrayDescriptionのルート型、Array<Int>は、stringLengthの値型、Intと合致しないからです。