Declaring unary operator functions

As previously explained, Swift 3 removed the prefix increment and postfix increment operators. However, imagine that many members of our team have experience with other programming languages that provide these operators and they want to use them to increase the value of the age property of the different Animal instances. We can declare the following unary operators to simplify their lives while coding:

  • Prefix increment (++): We will use the operator before the variable to which it is applied (for example, ++pluto)
  • Postfix increment (++): We will use the operator after the variable to which it is applied (for example, pluto++)

In this case, both the operators use exactly the same characters; therefore, we must use ...

Get Swift 3 ObjectOriented Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.