Chapter    6

Operators

General-purpose languages provide set of operators—convenience constructs that behave like functions—and Swift is no different. You’ve already seen at least one such operator: +. Swift provides a wide range of built-in operators and allows you to define operators for your own classes and to extend built-in classes to add new operators.

Syntax

Operators in Swift can be:

  • Unary, which requires only one operand or input. (-a).
  • Binary, which requires two operands or inputs. (a+b).
  • Ternary, which requires three operands. (?:).

Notation

Operators come in one of three notations:

  • Prefix, where the operator comes before the operand(s). The operator can be either unary or binary. (++a)
  • Infix, where the operator requires two operands ...

Get Learn Swift 2 on the Mac, 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.