Using isKindOfClass with Swift
Solution 1
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
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
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
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
{
}
}
Related videos on Youtube
lkemitchll
Updated on October 13, 2020Comments
-
lkemitchll over 3 years
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 ... } }
-
Malcolm Jarvis almost 10 yearsForgot super in mine. You can also take out the
: UITouch
in this one, as type inference will know what it is by theas UITouch
cast -
Rui Peres almost 10 years@MalcolmJarvis it doesn't hurt having it.
-
Adam Fox almost 10 yearsThis 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
-
Florian Burel almost 10 yearsThis 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!
-
Adam Freeman over 8 yearsJust 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 { ... }
-
Kevin over 8 years@AdamFreeman in that case you'd be better off using
if touch.view is UIPickerView {...}
-
Kevin over 8 yearsAnd I'm not sure either
is
or_
existed at the point this question was asked, a few days after the language was first announced. -
Adam Freeman over 8 years@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
-
devios1 over 8 yearsStealing other users' rep by putting their answer in yours is in poor taste.
-
KPM about 8 years@Kevin it did exist, as the date of my answer demonstrates ;)
-
footyapps27 over 7 yearsWorks with Swift 3 as well!
-
Vyachaslav Gerchicov about 7 yearsdoesn't work. Suggests
UIPickerView.self
. Is it correct? -
Ravi over 5 yearsWorks with Swift 4.2 as well!
-
BigBoy1337 about 4 yearshow can you do this in a switch statement to check several different class types?