我正在尝试学习一些 Swift 语言,我想知道如何将以下 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 ...
}
}
最佳答案
正确的 Swift 运算符是是
:
if touch.view is UIPickerView {
// touch.view is of type UIPickerView
}
当然,如果你还需要给view赋值一个新的常量,那么if let ... as? ...
语法是你的男孩,正如凯文提到的那样。但如果您不需要该值而只需要检查类型,那么您应该使用 is
运算符。
关于ios - 在 Swift 中使用 isKindOfClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24019707/