簡體   English   中英

Swift 3 - 如何驗證對象的類類型

[英]Swift 3 - How to verify class type of object

這行代碼曾經適用於 Swift 2,但現在在 Swift 3 中不正確。

if gestureRecognizer.isMember(of: UITapGestureRecognizer) { }

我收到此錯誤:類型名稱后的預期成員名稱或構造函數調用。

使用isMember(of:)的正確方法是什么?

很可能,您不僅要檢查類型,還要強制轉換為該類型。 在這種情況下,請使用:

if let gestureRecognizer as? UITapGestureRecognizer { }
else { /* not a UITapGestureRecognizer */ }

Swift 轉換運算符

這些運算符僅在 Swift 中可用,但在處理 Objective C 類型時仍然有效。

  • as運算符

    • 當在編譯時知道強制轉換總是成功時, as運算符會執行強制轉換,例如向上轉換或橋接。 Upcasting 允許您將表達式用作其類型超類型的實例,而無需使用中間變量。

    • 如果可能,這是最可取的操作符。 它保證成功,而無需擔心打開可選組件或冒崩潰的風險。
  • as? 操作員

    • as? 運算符將表達式有條件地轉換為指定的類型。 as? 運算符返回指定類型的可選項。 在運行時,如果轉換成功,則表達式的值被包裝在一個可選項中並返回; 否則,返回的值為nil 如果強制轉換為指定的類型一定會失敗或一定會成功,則會引發編譯時錯誤。

    • 這是第二個最適合使用的運算符。 使用它可以安全地處理無法執行鑄造操作員的情況。

  • as! 操作員

    • as! 運算符將表達式強制轉換為指定類型。 as! 運算符返回指定類型的值,而不是可選類型。 如果轉換失敗,則會引發運行時錯誤。 x as! T的行為x as! T x as! T(x as? T)!的行為相同(x as? T)! .

    • 這是最不推薦使用的運算符。 我強烈建議不要濫用它。 嘗試將表達式轉換為不兼容的類型會導致程序崩潰。


Swift 類型檢查

如果您只想檢查表達式的類型,而不是強制轉換為該類型,那么您可以使用這些方法。 它們僅在 Swift 中可用,但在處理 Objective C 類型時仍然有效。

  • is運算符

    • is運算符在運行時檢查表達式是否可以轉換為指定的類型。 如果表達式可以轉換為指定的類型,則返回true 否則返回false
    • 適用於任何 Swift 類型,包括 Objective C 類型。
    • Swift 相當於isKind(of:)
  • 使用type(of:)

    • is運算符不同,這可用於檢查確切類型,而無需考慮子類。
    • 可以像這樣使用: type(of: instance) == DesiredType.self
    • Swift 相當於isMember(of:)

用於檢查類型的傳統(Objective C)方法

這些都是NSObjectProtocol方法。 它們可以在 Swift 代碼中使用,但它們只適用於派生自NSObjectProtocol類(例如NSObject子類)。 我建議不要使用這些,但我在這里提到它們是為了完整性

  • isKind(of:)

    • 返回一個布爾值,指示接收者是給定類的實例還是從該類繼承的任何類的實例
    • 在 Swift 中避免這種情況,請改用is運算符。
  • isMember(of:)

    • 返回一個布爾值,指示接收者是否是給定類的實例
    • 在 Swift 中避免這種情況,使用type(of: instance) == DesiredType.self代替。
  • conforms(to:)

    • 返回一個布爾值,指示接收者是否符合給定的協議。
    • 在 Swift 中避免這種情況,請改用is運算符。

有幾種方法可以檢查對象的類。 大多數情況下,您希望使用isas? 像這樣的運營商:

let gestureRecognizer: UIGestureRecognizer = UITapGestureRecognizer()

// Using the is operator
if gestureRecognizer is UITapGestureRecognizer {
    // You know that the object is an instance of UITapGestureRecognizer,
    // but the compiler will not let you use UITapGestureRecognizer specific
    // methods or properties on gestureRecognizer because the type of the
    // variable is still UIGestureRecognizer
    print("Here")
}

// Using the as? operator and optional binding
if let tapGestureRecognizer = gestureRecognizer as? UITapGestureRecognizer {
    // tapGestureRecognizer is the same object as gestureRecognizer and is
    // of type UITapGestureRecognizer, you can use UITapGestureRecognizer
    // specific methods or properties.
    print("Here")
}

// Using the type(of:) global function
if type(of: gestureRecognizer) == UITapGestureRecognizer.self {
    // gestureRecognizer is an instance of UITapGestureRecognizer, but not any
    // of its subclasses (if gestureRecognizer was an instance of a subclass of
    // UITapGestureRecognizer, the body of this if would not execute).
    // This kind of check is rarely usefull, be sure this is really what you
    // want to do before you use it.
    print("Here")
}

// Using the isKind(of:) method
if gestureRecognizer.isKind(of: UITapGestureRecognizer.self) {
    // Like for the is operator, you know that the object is an instance of
    // UITapGestureRecognizer (or any subclass of UITapGestureRecognizer).
    // This is the Objective-C version of the is operator and will only work
    // on classes that inherit from NSObject, don't use it in Swift.
    print("Here")
}

// Using the isMember(of:) method
if gestureRecognizer.isMember(of: UITapGestureRecognizer.self) {
    // gestureRecognizer is an instance of UITapGestureRecognizer, but not
    // any of its subclasses.
    // This is the Objective-C version of type(of:) and will only work on
    // classes that inherit from NSObject, don't use it in Swift.
    print("Here")
}

您現在必須使用 .self 來引用類類型。

let a = UITapGestureRecognizer()
print (a.isMember(of: UIGestureRecognizer.self))

還有:

print (a is UITapGestureRecognizer)

斯威夫特 3:

if gestureRecognizer is UITapGestureRecognizer {
            //It's a tap
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM