實例方法
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:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。