Using isKindOfClass with Swift

IosSwiftReflectionIntrospection

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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)
    {
        
    }
}

Edit

As pointed out in @Kevin's answer, the correct way would be to use optional type cast operator as?. You can read more about it on the section Optional Chaining sub section Downcasting.

Edit 2

As pointed on the other answer by user @KPM, using the is operator is the right way to do it.

Solution 3 - Ios

You can combine the check and cast into one statement:

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

Then you can use picker within the if block.

Solution 4 - Ios

I would use:

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
    {

    }
}

Solution 5 - Ios

Another approach using the new Swift 2 syntax is to use guard and nest it all in one conditional.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlkemitchllView Question on Stackoverflow
Solution 1 - IosKPMView Answer on Stackoverflow
Solution 2 - IosRui PeresView Answer on Stackoverflow
Solution 3 - IosKevinView Answer on Stackoverflow
Solution 4 - IoscrazynameView Answer on Stackoverflow
Solution 5 - IosSam CorderView Answer on Stackoverflow