Operators are a key ingredient to every app. In fact, it’s difficult to write an app—other than the standard “Hello, world!” example—that doesn’t contain at least one operator. Operators are symbols or a set of symbols that change or assign values, combine values, or check or verify values in your code. In this hour, we learn about most of the basic operators in Swift and understand why and how to use them.
All operators in Swift can be categorized as unary, binary, or ternary. Unary operators perform an action upon a single operand, binary operators perform an action upon two operands, and ternary operators perform an action involving three operands.
A unary operator operates on a single value. A unary operator can be prefix or postfix, meaning that it can come before a variable or constant (prefix, such as ++count), or immediately follow a variable or constant (postfix, such as count++). Some unary operators can be either (prefix or postfix), while some can be only one or the other. A unary operator cannot have any whitespace between itself and the variable or constant. Unary operators act upon numeric and Boolean types in Swift. Let’s take a look at Swift’s unary operators.
Increment and Decrement Operators
Two similar operators are the increment operator and the decrement operator. The increment operator, denoted by ++, increases a numeric value by 1, and the decrement operator, denoted by --, decreases a numeric value by 1. The increment operator is short-hand for a longer expression; a++ is the same as a = a + 1, and a-- is the same as a = a - 1. Both the increment and decrement operators can be prefix or postfix.
There is a key difference in behavior, however, between the increment and decrement operators concerning prefix and postfix. That is the order in which the value is incremented or decremented and when assignment occurs. A prefixed decrement operator decrements the numeric value by 1 and returns the newly assigned value. Likewise, a prefixed increment operator increments the numeric value by 1 and returns the newly assigned value. The postfixed decrement operator first returns the numeric value before decrementing; likewise, the postfixed increment operator returns the numeric value before incrementing. Take a look at the following code example to see this behavior in action:
var x = 1 print(++x) //++x - x increments first, new value is displayed x = 1 //just resetting value for demonstration print(x++) //now x is incremented, but after the print function has already occurred print(x) //notice the different value now when this line is printed?
Logical NOT Operator
The logical NOT operator inverts the value of a Boolean variable. In other words, if a variable’s value is true, the variable will now be false, and vice versa. The logical NOT operator is always a prefix operator. We discuss the logical NOT operator and other logical operators a little more in depth later in this hour. Here is what the logical NOT operator looks like in code:
let a = true // a is a Bool equaling true let b = !a // b is a Bool equaling false
Unary Minus Operator
The unary minus operator is short-hand for multiplying a variable’s value by -1, and returning the result. The unary minus operator is always a prefix operator and can prefix variables, constants, or numeric literals. Here is what the unary minus operator looks like in code:
let c = 5 let d = -c // d is equal to -5 let e = -42 // e is equal to -42
Swift also has a unary plus operator, by prefixing a + before any variable or constant, but it doesn’t actually change the value of a numeric variable, because any negative multiplied by a positive is still a negative. The unary plus operator is more for distinction in your code to show that a certain value should be positive. It is very rarely used, but it is available.