Using isKindOfClass with Swift

introspection, ios, reflection, swift

Solution

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.

Problem

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 } } ``` 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 ... } } ```

Original source