I am using a 3rd party framework, there is a file contains the following code:
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!()
}
Then, in my own project class, I have
var aService : AdService?
//aService is initialised
//COMPILER ERROR: Binary operator ’==’ cannot be applied to two AdServiceType operands
if aService!.serviceType == Normal {
//DO SOMETHING
}
I got the compiler error mentioned above when I check if serviceType
is Normal
. Why? How to get rid of it?
The AdServiceType
struct is not Equatable
and cannot be used as a switch expression. But its value
can. Try:
switch aService!.serviceType.value {
case Internal.value:
//do something
case Normal.value:
//do something
case External.value:
//do something
default:
...
}
Alternatively, you can extend AdServiceType
adding support for the Equatable
protocol:
extension AdServiceType : Equatable {}
public func ==(lhs: AdServiceType, rhs: AdServiceType) -> Bool
{
return lhs.value == rhs.value
}
and leave the switch as it is.
I would fix it by making the struct Equatable:
extension AdServiceType: Equatable {}
func ==(lhs: AdServiceType, rhs: AdServiceType) -> Bool {
return lhs.value == rhs.value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With