ios 在 Swift 中使用 isKindOfClass

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24019707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:19:00  来源:igfitidea点击:

Using isKindOfClass with Swift

iosswiftreflectionintrospection

提问by lkemitchll

I'm trying to pick up a bit of Swift lang and I'm wondering how to convert the following Objective-C into Swift:

我正在尝试学习一些 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
    }
}

More specifically I need to know how to use isKindOfClassin the new syntax.

更具体地说,我需要知道如何isKindOfClass在新语法中使用。

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    ???

    if ??? {
        // your touch was in a uipickerview ...

    }
}

回答by KPM

The proper Swift operator is is:

正确的 Swift 运算符是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 isoperator.

当然,如果你还需要将视图分配给一个新的常量,那么if let ... as? ...语法就是你的孩子,正如凯文所提到的。但是如果你不需要值而只需要检查类型,那么你应该使用is运算符。

回答by Rui Peres

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 Chainingsub section Downcasting.

正如@Kevin 的回答中指出的那样,正确的方法是使用可选的类型转换运算符as?。您可以在部分Optional Chaining子部分阅读更多关于它的信息Downcasting

Edit 2

编辑 2

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

正如用户@KPM另一个答案中指出的那样,使用is运算符是正确的方法。

回答by Kevin

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 pickerwithin the ifblock.

然后你可以pickerif块内使用。

回答by crazyname

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
    {

    }
}

回答by Sam Corder

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

使用新 Swift 2 语法的另一种方法是使用保护并将其全部嵌套在一个条件中。

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