[英]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 中可用,但在處理 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 中可用,但在處理 Objective C 類型時仍然有效。
is
運算符is
運算符在運行時檢查表達式是否可以轉換為指定的類型。 如果表達式可以轉換為指定的類型,則返回true
; 否則返回false
isKind(of:)
type(of:)
is
運算符不同,這可用於檢查確切類型,而無需考慮子類。type(of: instance) == DesiredType.self
isMember(of:)
這些都是NSObjectProtocol
方法。 它們可以在 Swift 代碼中使用,但它們只適用於派生自NSObjectProtocol
類(例如NSObject
子類)。 我建議不要使用這些,但我在這里提到它們是為了完整性
isKind(of:)
is
運算符。isMember(of:)
type(of: instance) == DesiredType.self
代替。conforms(to:)
is
運算符。有幾種方法可以檢查對象的類。 大多數情況下,您希望使用is
或as?
像這樣的運營商:
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.