简体   繁体   English

如何将OR运算符与可选绑定一起使用?

[英]How can I use the OR operator with optional binding?

I have a piece of code that I would like to execute if a variable can be casted as one of two types. 如果有一个变量可以转换为两种类型之一,我有一段代码要执行。

if let myOptional = variableToCast as! firstTypeToTryToCastAs || 
   let myOptional = variableToCast as! secondTypeToTryToCastAs {
        //Execute some code
}

However, Swift (as of version 2.0) apparently does not allow this. 但是,Swift(从2.0版开始)显然不允许这样做。 I'm looking for a way to do this without having to create two separate if blocks. 我正在寻找一种无需创建两个单独的if块的方法。

My code only uses the super type, therefore the code dealing with both types are the same. 我的代码仅使用超级类型,因此处理这两种类型的代码是相同的。 However, I can't cast it to the super type because I don't want to code to execute if variableToCast is one of the many other possible types that are also derived from the same super type. 但是,我不能将其强制转换为超级类型,因为如果variableToCast是从相同超级类型派生的许多其他可能类型之一,我也不想执行代码。

Based on your comment: 根据您的评论:

My code only uses the super type, therefore the code dealing with both types are the same. 我的代码仅使用超级类型,因此处理这两种类型的代码是相同的。 However, I can't cast it to the super type because I don't want to code to execute if variableToCast is one of the many other possible types that are also derived from the same super type. 但是,我不能将其强制转换为超级类型,因为如果variableToCast是从相同超级类型派生的许多其他可能类型之一,我也不想执行代码。

In your optional binding if let cast to the super type and then limit it to the classes you are interested in with a where clause: 可选绑定中, if let强制转换为超类型,然后使用where子句将其限制为您感兴趣的类:

class SuperType {

}

class firstTypeToTryToCastAs: SuperType {

}

class secondTypeToTryToCastAs: SuperType {

}

class thirdTypeToTryToCastAs: SuperType {

}

var variableToCast: AnyObject = firstTypeToTryToCastAs()

if let myOptional = variableToCast as? SuperType where myOptional is firstTypeToTryToCastAs || myOptional is secondTypeToTryToCastAs {
    print("this works")
} else {
    print("not the type we are looking for")
}

I'm looking for a way to do this without having to create two separate if blocks 我正在寻找一种无需创建两个单独的if块的方法

Why? 为什么?

You can't use the same code in the if block anyway because myOptional will be of a different type. 无论如何,您不能在if块中使用相同的代码,因为myOptional将具有不同的类型。 I would go with 我会去

if let myOptional = variableToCast as! firstTypeToTryToCastAs
{
    // code dealing with a firstTypeToTryToCastAs
}
else if let myOptional = variableToCast as! secondTypeToTryToCastAs
{
    // code dealing with a secondTypeToTryToCastAs
}

On the other hand if your two types have a common super type, cast it to that super type. 另一方面,如果您的两个类型具有共同的超级类型,则将其强制转换为该超级类型。

To add onto JeremyP's answer, if you don't actually care what the value of myOptional is, but just want to execute some code if it is one of those two types, you could do this instead 要添加到JeremyP的答案中,如果您实际上并不关心myOptional的值是什么,而只想执行某些代码(如果这是这两种类型之一),则可以执行此操作

if variableToCast is firstTypeToTryToCastAs || variableToCast is secondTypeToTryToCastAs {
    //Code for both cases
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM