245

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

    }
}
norq
  • 1,404
  • 2
  • 18
  • 35
lkemitchll
  • 2,751
  • 2
  • 15
  • 16

5 Answers5

518

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.

KPM
  • 10,558
  • 3
  • 45
  • 66
134
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.

Community
  • 1
  • 1
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
50

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.

Kevin
  • 53,822
  • 15
  • 101
  • 132
  • 3
    This is the "more correct" answer because it uses the Swift 'as?' operator. The documentation states that "In Objective-C, you use the isKindOfClass: method to check whether an object is of a certain class type, and the conformsToProtocol: method to check whether an object conforms to a specified protocol. In Swift, you accomplish this task by using the is operator to check for a type, or the as? operator to downcast to that type." [Apple Documentation](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html) – Adam Fox Jun 05 '14 at 15:21
  • This is the right way of performing isKingOfClass in swift. You will enter the if block only if the object is of class UIPickerView! Great answer! – Florian Burel Jun 19 '14 at 00:32
  • Just wanted to point out that if you just need to do the check and not use 'picker' in the subsequent 'if' block, then you would want to use: if let _ = touch.view as? UIPickerView { ... } – Adam Freeman Nov 07 '15 at 00:38
  • @AdamFreeman in that case you'd be better off using `if touch.view is UIPickerView {...}` – Kevin Nov 17 '15 at 05:52
  • And I'm not sure either `is` or `_` existed at the point this question was asked, a few days after the language was first announced. – Kevin Nov 17 '15 at 05:54
  • @Kevin I was just trying to let people know about the underscore '_' character and that it can be used for a parameter that is not used in the block you don't have to jump all over my answer – Adam Freeman Nov 17 '15 at 06:14
  • @Kevin it did exist, as the date of my answer demonstrates ;) – KPM May 07 '16 at 03:12
1

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
    {

    }
}
crazyname
  • 135
  • 1
  • 15
-2

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
Sam Corder
  • 5,374
  • 3
  • 25
  • 30