CarpenterBlood
CarpenterBlood

Reputation: 151

How to add or subtract two enum values in swift

So I have this enum that defines different view positions on a View controller when a side bar menu is presented. I need to add, subtract, multiply, or divide the different values based on different situations. How exactly do I form a method to allow me to use -, +, *, or / operators on the values in the enum. I can find plenty examples that use the compare operator ==. Although I haven't been able to find any that use >=. Which I also need to be able to do.

Here is the enum

enum FrontViewPosition: Int {
    case None
    case LeftSideMostRemoved
    case LeftSideMost
    case LeftSide
    case Left
    case Right
    case RightMost
    case RightMostRemoved
}

Now I'm trying to use these operators in functions like so.

func getAdjustedFrontViewPosition(_ frontViewPosition: FrontViewPosition, forSymetry symetry: Int) {
    var frontViewPosition = frontViewPosition
    if symetry < 0 {
        frontViewPosition = .Left + symetry * (frontViewPosition - .Left)
    }
}

Also in another function like so.

func rightRevealToggle(animated: Bool) {
    var toggledFrontViewPosition: FrontViewPosition = .Left
    if self.frontViewPosition >= .Left {
        toggledFrontViewPosition = .LeftSide
    }
    self.setFrontViewPosition(toggledFrontViewPosition, animated: animated)
}

I know that i need to directly create the functions to allow me to use these operators. I just don't understand how to go about doing it. A little help would be greatly appreciated.

Upvotes: 0

Views: 1427

Answers (2)

CRD
CRD

Reputation: 53010

The type you are trying to define has a similar algebra to pointers in that you can add an offset to a pointer to get a pointer and subtract two pointers to get a difference. Define these two operators on your enum and your other functions will work.

Any operators over your type should produce results in your type. There are different ways to achieve this, depending on your requirements. Here we shall treat your type as a wrap-around ("modulo") one - add 1 to the last literal and you get the first. To do this we use raw values from 0 to n for your types literals and use modulo arithmetic.

First we need a modulo operator which always returns a +ve result, the Swift % can return a -ve one which is not what is required for modulo arithmetic.

infix operator %% : MultiplicationPrecedence
func %%(_ a: Int, _ n: Int) -> Int
{
   precondition(n > 0, "modulus must be positive")
   let r = a % n
   return r >= 0 ? r : r + n
}

Now your enum assigning suitable raw values:

enum FrontViewPosition: Int
{
   case None = 0
   case LeftSideMostRemoved = 1
   case LeftSideMost = 2
   case LeftSide = 3
   case Left = 4
   case Right = 5
   case RightMost = 6
   case RightMostRemoved = 7

Now we define the appropriate operators.

For addition we can add an integer to a FrontViewPosition and get a FrontViewPosition back. To do this we convert to raw values, add, and then reduce modulo 8 to wrap-around. Note the need for a ! to return a non-optional FrontViewPosition - this will always succeed due to the modulo math:

   static func +(_ x : FrontViewPosition, _ y : Int) -> FrontViewPosition
   {
      return FrontViewPosition(rawValue: (x.rawValue + y) %% 8)!
   }

For subtraction we return the integer difference between two FrontViewPosition values:

   static func -(_ x : FrontViewPosition, _ y : FrontViewPosition) -> Int
   {
      return x.rawValue - y.rawValue
   }

}

You can define further operators as needed, say a subtraction operator which takes a FrontViewPosition and an Int and returns a FrontViewPosition.

HTH

Upvotes: 2

OmniBug
OmniBug

Reputation: 902

Enum could have function~

enum Tst:Int {
    case A = 10
    case B = 20
    case C = 30
    static func + (t1:Tst,t2:Tst) -> Tst {
        return Tst.init(rawValue: t1.rawValue+t2.rawValue)! //here could be wrong!
    }
}

var a = Tst.A
var b = Tst.B
var c = a+b

Upvotes: 1

Related Questions