Overview:
- I have 2 enums
Shape
andVehicle
as mentioned below
Question:
- How can I determine the boolean value if a certain instance of
Vehicle
is a car? - I have implemented a function called
isCar
, just wondering if there is a better way to get the boolean value? - Let me know if making it
Equatable
or any other way helps?
Note: Based on my requirement it needs to be a Bool
because the function accepts only accepts a Bool
enum Shape {
case circle
case square
case triangle
}
enum Vehicle {
case car(Shape)
case truck
var isCar: Bool {
if case .car = self {
return true
} else {
return false
}
}
}
let v1 : Vehicle = .car(.circle)
let b1: Bool = v1.isCar //true