实例方法
appending(path:)
返回通过将给定的 key 路径附加到此路径而创建的新 key 路径。
声明
func appending(path: AnyKeyPath) -> AnyKeyPath?
当
Self
为 AnyKeyPath
时可用。返回值
如果可以附加 path
,则从该键路径的根开始的键路径和 path
的值类型。如果无法追加 path
,则返回 nil
。
参数
path
要追加的关键路径。
详述
使用此方法将此键路径扩展为另一个键路径的值类型。仅当 path
的根类型与此键路径的值类型匹配时,才能成功附加作为 path
传递的键路径。此示例创建从 Array<Int>
到 String
以及从 String
到 Int
的关键路径,然后尝试将每个路径附加到另一个:
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
第二次调用 appending(path:)
返回 nil
因为 arrayDescription
的根类型 Array<Int>
与 stringLength
的值类型不匹配 Int
。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift AnySequence elementsEqual(_:)用法及代码示例
- Swift AnyCollection min()用法及代码示例
- Swift AnyBidirectionalCollection elementsEqual(_:)用法及代码示例
- Swift AnyRandomAccessCollection randomElement(using:)用法及代码示例
- Swift AnyBidirectionalCollection lexicographicallyPrecedes(_:)用法及代码示例
- Swift AnyHashable ==(_:_:)用法及代码示例
- Swift AnyBidirectionalCollection split(separator:maxSplits:omittingEmptySubsequences:)用法及代码示例
- Swift AnyRandomAccessCollection first(where:)用法及代码示例
- Swift AnyBidirectionalCollection sorted()用法及代码示例
- Swift AnyRandomAccessCollection min()用法及代码示例
- Swift AnySequence shuffled(using:)用法及代码示例
- Swift AnyBidirectionalCollection shuffled(using:)用法及代码示例
- Swift AnyRandomAccessCollection prefix(upTo:)用法及代码示例
- Swift AnyBidirectionalCollection starts(with:)用法及代码示例
- Swift AnyBidirectionalCollection contains(where:)用法及代码示例
- Swift AnyCollection allSatisfy(_:)用法及代码示例
- Swift AnyRandomAccessCollection prefix(through:)用法及代码示例
- Swift AnyBidirectionalCollection suffix(from:)用法及代码示例
- Swift AnyCollection compactMap(_:)用法及代码示例
- Swift AnyHashable debugDescription用法及代码示例
- Swift AnyIterator suffix(_:)用法及代码示例
- Swift AnyIterator lexicographicallyPrecedes(_:)用法及代码示例
- Swift AnySequence joined()用法及代码示例
- Swift AnyObject用法及代码示例
- Swift AnyRandomAccessCollection sorted()用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 AnyKeyPath appending(path:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。