type(of:)
聲明
func type<T, Metatype>(of value: T) -> Metatype
返回值
動態類型,它是一個元類型實例。
參數
value要為其查找動態類型的值。
詳述
您可以使用type(of:) 函數來查找值的動態類型,特別是當動態類型與靜態類型不同時。值的static type 是該值的已知編譯時類型。值的dynamic type 是該值在運行時的實際類型,可以是其具體類型的子類型。
在以下代碼中,count 變量具有相同的靜態和動態類型:Int。但是,當 count 傳遞給 printInfo(_:) 函數時,value 參數的靜態類型為 Any(為參數聲明的類型)和動態類型為 Int 。
func printInfo(_ value: Any) {
let t = type(of: value)
print("'\(value)' of type '\(t)'")
}
let count: Int = 5
printInfo(count)
// '5' of type 'Int'
從 type(of:) 返回的動態類型是類、結構、枚舉或其他非協議類型 T 的 concrete metatype ( T.Type ),或協議或協議組合的 existential metatype ( P.Type ) P 。當傳遞給 type(of:) 的值的靜態類型被限製為類或協議時,您可以使用該元類型來訪問類或協議的初始化程序或其他靜態成員。
例如,以下示例中作為value 傳遞給printSmileyInfo(_:) 函數的參數是Smiley 類或其子類之一的實例。該函數使用 type(of:) 來查找 value 的動態類型,它本身就是 Smiley.Type 元類型的一個實例。
class Smiley {
class var text: String {
return ":)"
}
}
class EmojiSmiley: Smiley {
override class var text: String {
return "😀"
}
}
func printSmileyInfo(_ value: Smiley) {
let smileyType = type(of: value)
print("Smile!", smileyType.text)
}
let emojiSmiley = EmojiSmiley()
printSmileyInfo(emojiSmiley)
// Smile! 😀
在此示例中,訪問 smileyType 元類型的 text 屬性將從 EmojiSmiley 子類而不是 Smiley 類的原始定義中檢索覆蓋的值。
在通用上下文中查找動態類型
通常,您不需要了解具體元類型和存在元類型之間的區別,但調用 type(of:) 可能會在類型參數綁定到協議的泛型上下文中產生意外結果。在這種情況下,泛型參數 T 綁定到協議 P ,類型參數在泛型函數的主體中不是靜態已知的協議類型。因此,type(of:) 隻能生成具體的元類型 P.Protocol 。
以下示例定義了一個 printGenericInfo(_:) 函數,該函數采用通用參數並聲明 String 類型與新協議 P 的一致性。當使用具有 P 作為其靜態類型的字符串調用 printGenericInfo(_:) 時,對 type(of:) 的調用將返回 P.self 而不是 String.self(參數內的動態類型)。
func printGenericInfo<T>(_ value: T) {
let t = type(of: value)
print("'\(value)' of type '\(t)'")
}
protocol P {}
extension String: P {}
let stringAsP: P = "Hello!"
printGenericInfo(stringAsP)
// 'Hello!' of type 'P'
出現此意外結果是因為在 printGenericInfo(_:) 中對 type(of: value) 的調用必須返回作為 T.Type 實例的元類型,但 String.self(預期的動態類型)不是 P.Type(具體元類型)的實例value )。要在此通用上下文中獲取 value 中的動態類型,請在調用 type(of:) 時將參數強製轉換為 Any 。
func betterPrintGenericInfo<T>(_ value: T) {
let t = type(of: value as Any)
print("'\(value)' of type '\(t)'")
}
betterPrintGenericInfo(stringAsP)
// 'Hello!' of type 'String'
可用版本
相關用法
- Swift transcode(_:from:to:stoppingOnError:into:)用法及代碼示例
- Swift KeyValuePairs flatMap(_:)用法及代碼示例
- Swift String.UTF8View first用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Optional.Publisher reduce(_:_:)用法及代碼示例
- Swift Int8 ~(_:)用法及代碼示例
- Swift SetAlgebra isStrictSubset(of:)用法及代碼示例
- Swift UInt +(_:)用法及代碼示例
- Swift Array enumerated()用法及代碼示例
- Swift FlattenSequence prefix(_:)用法及代碼示例
- Swift Slice endIndex用法及代碼示例
- Swift LazySequence split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift MutableCollection partition(by:)用法及代碼示例
- Swift ReversedCollection min(by:)用法及代碼示例
- Swift RandomNumberGenerator用法及代碼示例
- Swift Dictionary.Keys shuffled()用法及代碼示例
- Swift AnySequence elementsEqual(_:)用法及代碼示例
- Swift UInt &<<(_:_:)用法及代碼示例
- Swift Optional.Publisher tryDrop(while:)用法及代碼示例
- Swift DefaultIndices endIndex用法及代碼示例
- Swift Substring.UnicodeScalarView insert(contentsOf:at:)用法及代碼示例
- Swift LazyFilterSequence dropFirst(_:)用法及代碼示例
- Swift LazySequence suffix(from:)用法及代碼示例
- Swift ArraySlice starts(with:)用法及代碼示例
- Swift Int16.Words max()用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 type(of:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
