Declaring compound assignment operator functions
We also want to be able to increase the value of the age
property of the different Animal
instances by using the addition assignment operator (+=
). This operator is one of the compound assignment operators that Swift provides and it combines assignment (=
) with the addition operator (+
).
Note
Swift 3 removed both the pre-increment, post-increment, pre-decrement, and post-decrement operators. In other words, we cannot use ++
and --
in Swift 3. We might define a prefix increment and a postfix increment to increase the value of the age
property, but it doesn't make sense to define an operator that Swift 3 has removed. Instead, we will use the addition assignment operator.
We have to declare an operator function with +=
as its name, specify the Animal
type for the left
argument, and specify the Int
type for the right
argument. Thus, we have left
and right
as the arguments for the operator function. In this case, the function doesn't return a value...