0

I would like to declare an assignment operator on my custom class CGFloat01 so that I can use it like so:

var a:CGFloat01
var b:CGFloat

a = b

I tried the following, but it doesn't compile:

func = (lhs:CGFloat01, rhs:CGFloat) -> CGFloat01 {
    lhs.value = rhs
    return lhs
}
andrewz
  • 4,729
  • 5
  • 49
  • 67

1 Answers1

2

You can't

From the Swift Documentation

It is not possible to overload the default assignment operator (=). Only the compound assignment operators can be overloaded. Similarly, the ternary conditional operator (a ? b : c) cannot be overloaded.

Mgetz
  • 5,108
  • 2
  • 33
  • 51