我试图拿起一点Swift lang,我想知道如何将以下Objective-C转换为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。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
???
if ??? {
// your touch was in a uipickerview ...
}
}
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
正如@Kevin的回答中指出的那样,正确的方法是使用可选的类型转换操作符作为?你可以阅读更多关于它的章节可选链子章节下casting。
编辑2
正如用户@KPM在另一个回答中指出的那样,使用is操作符是正确的方法。
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
正如@Kevin的回答中指出的那样,正确的方法是使用可选的类型转换操作符作为?你可以阅读更多关于它的章节可选链子章节下casting。
编辑2
正如用户@KPM在另一个回答中指出的那样,使用is操作符是正确的方法。