将isKindOfClass与Swift一起使用 [英] Using isKindOfClass with Swift

查看:379
本文介绍了将isKindOfClass与Swift一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试接收一些Swift lang,我想知道如何将以下Objective-C转换为Swift:

I'm trying to pick up a bit of Swift lang and I'm wondering how to convert the following Objective-C into 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 在新语法中。

More specifically I need to know how to use isKindOfClass in the new syntax.

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

    ???

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

    }
}


推荐答案

正确的Swift运算符

The proper Swift operator is is:

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

当然,如果您还需要将视图分配给新常量,那么如果让...作为? ... 语法是你的孩子,凯文提到。但是如果您不需要该值并且只需要检查类型,那么您应该使用运算符。

Of course, if you also need to assign the view to a new constant, then the if let ... as? ... syntax is your boy, as Kevin mentioned. But if you don't need the value and only need to check the type, then you should use the is operator.

这篇关于将isKindOfClass与Swift一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆