MyBikeIsAwesome MyBikeIsAwesome - 1 year ago 56
Swift Question

How to make a custom relational operator in Swift

I am working with Doubles that have a range of

. I want to create a switch statement that will have 8 cases. A case for 0, 90, 180 and 270, and a case for the values in between. It seems that the half-open operator will not satisfy what I'm trying to do here. I'm thinking the solution may be to create a custom relational operator. Let's call it

case 0:
case 0<..<90:
case 90:
case 90<..<180:
case 180:
case 180<..<270:
case 270:
case 270<..<360:

I've looked into creating a custom infix operator that returns a
but that won't work because the arguments would only include the lhs and rhs.

How can I accomplish this?

Answer Source

Your switch is unnecessarily complicated. See if this works for you:

let value: Double = 180

switch value {
    case 0:
        print("exactly 0")
    case 0..<90:
        print("less than 90")
    case 90:
        print("exactly 90")
    case 90..<180:
        print("less than 180")
    case 180:
        print("exactly 180")
    case 180..<270:
        print("less than 270")
    case 270:
        print("exactly 270")
        print("less than 360")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download