實例方法
descendant(_:
descendant(_:_:)
返回反射主題的特定後代,如果不存在這樣的後代,則返回
nil
。聲明
func descendant(
_ first: MirrorPath,
_ rest: MirrorPath...
) -> Any?
返回值
如果存在這樣的後代,則由給定的鏡像路徑組件指定此鏡像的後代;否則,nil
。
參數
first
要訪問的第一個鏡像路徑組件。
rest
任何剩餘的鏡像路徑組件。
詳述
傳遞字符串和整數參數的可變參數列表。每個字符串參數選擇具有匹配標簽的第一個孩子。每個整數參數都選擇該偏移處的孩子。例如,將 1, "two", 3
作為參數傳遞給 myMirror.descendant(_:_:)
等效於:
var result: Any? = nil
let children = myMirror.children
if let i0 = children.index(
children.startIndex, offsetBy: 1, limitedBy: children.endIndex),
i0 != children.endIndex
{
let grandChildren = Mirror(reflecting: children[i0].value).children
if let i1 = grandChildren.firstIndex(where: { $0.label == "two" }) {
let greatGrandChildren =
Mirror(reflecting: grandChildren[i1].value).children
if let i2 = greatGrandChildren.index(
greatGrandChildren.startIndex,
offsetBy: 3,
limitedBy: greatGrandChildren.endIndex),
i2 != greatGrandChildren.endIndex
{
// Success!
result = greatGrandChildren[i2].value
}
}
}
此函數適用於探索 REPL 或遊樂場中的鏡像結構,但並非旨在提高效率。在參數列表中查找每個元素的效率取決於參數類型和鏡像children
集合的每個級別的函數。每個字符串參數都需要線性搜索,除非底層集合支持隨機訪問遍曆,否則每個整數參數也需要線性操作。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift Mirror description用法及代碼示例
- Swift Mirror.Children用法及代碼示例
- Swift Mirror.AncestorRepresentation.customized(_:)用法及代碼示例
- Swift Mirror用法及代碼示例
- Swift MutableCollection partition(by:)用法及代碼示例
- Swift MutableCollection subscript(_:)用法及代碼示例
- Swift MemoryLayout alignment(ofValue:)用法及代碼示例
- Swift MemoryLayout用法及代碼示例
- Swift MutableCollection sort(by:)用法及代碼示例
- Swift ManagedBufferPointer用法及代碼示例
- Swift MutableCollection shuffle()用法及代碼示例
- Swift MemoryLayout stride(ofValue:)用法及代碼示例
- Swift MemoryLayout offset(of:)用法及代碼示例
- Swift MutableCollection用法及代碼示例
- Swift MutableCollection reverse()用法及代碼示例
- Swift MemoryLayout size(ofValue:)用法及代碼示例
- Swift MutableCollection sort()用法及代碼示例
- Swift MutableCollection shuffle(using:)用法及代碼示例
- Swift KeyValuePairs flatMap(_:)用法及代碼示例
- Swift String.UTF8View first用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Optional.Publisher reduce(_:_:)用法及代碼示例
- Swift Int8 ~(_:)用法及代碼示例
- Swift SetAlgebra isStrictSubset(of:)用法及代碼示例
- Swift UInt +(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Mirror descendant(_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。