Swift 3 - 如何验证对象的类类型

Van*_*ran 34 casting type-inference ios swift3

这行代码用于使用Swift 2,但现在在Swift 3中不正确.

if gestureRecognizer.isMember(of: UITapGestureRecognizer) { }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:类型名称后的预期成员名称或构造函数调用.

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

Ale*_*ica 119

最有可能的是,您不仅要检查类型,还要转换为该类型.在这种情况下,使用:

if let gestureRecognizer as? UITapGestureRecognizer { }
else { /* not a UITapGestureRecognizer */ }
Run Code Online (Sandbox Code Playgroud)

Swift铸造操作员

这些运算符仅在Swift中可用,但在处理Objective C类型时仍然有效.

  • as运营商

    • as当在该铸造总是成功,如向上转型或桥接编译时已知操作者进行的铸造.Upcasting允许您使用表达式作为其类型的超类型的实例,而无需使用中间变量.

    • 在可能的情况下,这是最优选的运算符.它保证了成功,而不用担心打开一个可选的或冒着崩溃的危险.
  • as?运营商

    • as?操作者进行表达的条件铸造为指定的类型.的as?操作者返回指定类型的可选.在运行时,如果转换成功,表达式的值将包装在一个可选的并返回; 否则,返回的值是nil.如果保证转换为指定类型失败或保证成功,则会引发编译时错误.

    • 这是第二个最优选的运算符.用它来安全地处理无法执行铸造操作的情况.

  • as!运营商

    • as!操作者进行的表达的强制投为指定的类型.的as!操作者返回指定的值类型,而不是一个可选类型.如果强制转换失败,则会引发运行时错误.行为与行为x as! T相同(x as? T)!.

    • 这是最不优选的运算符.我强烈建议不要滥用它.尝试将表达式强制转换为不兼容的类型会导致程序崩溃.


Swift类型检查

如果您只想检查表达式的类型而不转换为该类型,则可以使用这些方法.它们仅在Swift中可用,但在处理Objective C类型时仍然有效.

  • is运营商

    • is在运行时操作者检查是否表达可以转换为指定的类型.true如果表达式可以强制转换为指定的类型,则返回; 否则,它返回false
    • 适用于任何Swift类型,包括Objective C类型.
    • 斯威夫特相当于 isKind(of:)
  • 运用 type(of:)

    • is运算符不同,这可用于检查确切类型,而无需考虑子类.
    • 可以像: type(of: instance) == DesiredType.self
    • 斯威夫特相当于 isMember(of:)

用于检查类型的旧(Objective C)方法

这些都是方法NSObjectProtocol.它们可以在Swift代码中使用,但它们仅适用于派生自NSObjectProtocol(例如子类NSObject)的类.我建议不要使用这些,但我在这里提到它们是为了完整性

  • isKind(of:)

    • 返回一个布尔值,指示接收者是给定类的实例还是从该类继承的任何类的实例
    • 在Swift中避免这种情况,is改为使用运算符.
  • isMember(of:)

    • 返回一个布尔值,指示接收者是否是给定类的实例
    • 是一个方法type(of: instance) == DesiredType.self,因此只适用于派生自的类conforms(to:)(如子类is)
    • 避免在Swift中使用它,as而是使用它.
  • as

    • 返回一个布尔值,指示接收器是否符合给定协议.
    • 是一个方法as?,因此只适用于派生自的类as?(如子类as?)
    • 在Swift中避免这种情况,nil改为使用运算符.


dea*_*eef 20

有几种方法可以检查对象的类.大多数时候你会想要使用或者像这样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")
}
Run Code Online (Sandbox Code Playgroud)


Jos*_*ann 5

你现在必须使用.self来引用类类型.

let a = UITapGestureRecognizer()
print (a.isMember(of: UIGestureRecognizer.self))
Run Code Online (Sandbox Code Playgroud)

还有:

print (a is UITapGestureRecognizer)
Run Code Online (Sandbox Code Playgroud)