我使用的是第三方框架,有一个文件包含以下代码:
struct AdServiceType {
init(_ value: UInt)
var value: UInt
}
var Internal: AdServiceType { get }
var Normal: AdServiceType { get }
var External: AdServiceType { get }
class AdService : NSObject {
var serviceType: AdServiceType
init!()
}
Run Code Online (Sandbox Code Playgroud)
然后,在我自己的项目课中,我有
var aService : AdService?
//aService is initialised
//COMPILER ERROR: Binary operator ’==’ cannot be applied to two AdServiceType operands
if aService!.serviceType == Normal {
//DO SOMETHING
}
Run Code Online (Sandbox Code Playgroud)
当我检查是否serviceType是时,我得到了上面提到的编译器错误Normal.为什么?如何摆脱它?
该AdServiceType结构不是Equatable也不能用作开关表达式.但它value可以.尝试:
switch aService!.serviceType.value {
case Internal.value:
//do something
case Normal.value:
//do something
case External.value:
//do something
default:
...
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以扩展AdServiceType添加对Equatable协议的支持:
extension AdServiceType : Equatable {}
public func ==(lhs: AdServiceType, rhs: AdServiceType) -> Bool
{
return lhs.value == rhs.value
}
Run Code Online (Sandbox Code Playgroud)
并按原样保留开关.
我发现最简单的解决方案是比较 的struct属性的值:
if aService!.serviceType.value == Normal.value {
//DO SOMETHING.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2191 次 |
| 最近记录: |