【问题标题】:Using isKindOfClass with Swift在 Swift 中使用 isKindOfClass
【发布时间】:2014-07-24 01:21:54
【问题描述】:

我正在尝试学习一些 Swift 语言,我想知道如何将以下 Objective-C 转换为 Swift:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];

    if ([touch.view isKindOfClass: UIPickerView.class]) {
      //your touch was in a uipickerview ... do whatever you have to do
    }
}

更具体地说,我需要知道如何在新语法中使用isKindOfClass

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    ???

    if ??? {
        // your touch was in a uipickerview ...

    }
}

【问题讨论】:

    标签: ios swift reflection introspection


    【解决方案1】:
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    
        super.touchesBegan(touches, withEvent: event)
        let touch : UITouch = touches.anyObject() as UITouch
    
        if touch.view.isKindOfClass(UIPickerView)
        {
    
        }
    }
    

    编辑

    正如@Kevin's answer 中所指出的,正确的方法是使用可选类型转换运算符as?。您可以在Optional Chaining 子部分Downcasting 上阅读更多相关信息。

    编辑 2

    正如other answer by user @KPM 所指出的,使用is 运算符是正确的方法。

    【讨论】:

    • 忘记了超级在我的。你也可以在这个中取出: UITouch,因为类型推断会通过as UITouch cast知道它是什么
    • @MalcolmJarvis 拥有它并没有什么坏处。
    • 通过将其他用户的答案放在您的答案中来窃取其他用户的代表是低俗的。
    • 不起作用。建议UIPickerView.self。对吗?
    【解决方案2】:

    您可以将检查和强制转换为一个语句:

    let touch = object.anyObject() as UITouch
    if let picker = touch.view as? UIPickerView {
        ...
    }
    

    然后您可以在if 块内使用picker

    【讨论】:

    • 这是“更正确”的答案,因为它使用 Swift 'as?'操作员。文档指出“在 Objective-C 中,您使用 isKindOfClass: 方法来检查对象是否属于某个类类型,并使用conformsToProtocol: 方法来检查对象是否符合指定的协议。在 Swift 中,您可以完成此操作通过使用 is 运算符检查类型或使用 as? 运算符向下转换为该类型来执行任务。” Apple Documentation
    • 这是在 swift 中执行 isKingOfClass 的正确方法。仅当对象属于 UIPickerView 类时,您才会进入 if 块!很好的答案!
    • 只是想指出,如果您只需要进行检查而不在随后的 'if' 块中使用 'picker',那么你会想要使用: if let _ = touch.view作为? UIPickerView { ... }
    • @AdamFreeman 在这种情况下你最好使用if touch.view is UIPickerView {...}
    • 我不确定 is_ 在该语言首次公布几天后是否存在。
    【解决方案3】:

    正确的 Swift 运算符是is

    if touch.view is UIPickerView {
        // touch.view is of type UIPickerView
    }
    

    当然,如果您还需要将视图分配给一个新常量,那么 if let ... as? ... 语法就是您的孩子,正如 Kevin 所提到的。但是如果你不需要值,只需要检查类型,那么你应该使用is 操作符。

    【讨论】:

    • 也适用于 Swift 3!
    • 也适用于 Swift 4.2!
    • 如何在 switch 语句中检查几种不同的类类型?
    【解决方案4】:

    使用新的 Swift 2 语法的另一种方法是使用保护并将其全部嵌套在一个条件中。

    guard let touch = object.AnyObject() as? UITouch, let picker = touch.view as? UIPickerView else {
        return //Do Nothing
    }
    //Do something with picker
    

    【讨论】:

      【解决方案5】:

      我会使用:

      override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
      
          super.touchesBegan(touches, withEvent: event)
          let touch : UITouch = touches.anyObject() as UITouch
      
          if let touchView = touch.view as? UIPickerView
          {
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-13
        相关资源
        最近更新 更多