Swift's Operators A Comprehensive Guide

Swift, Apple’s powerful and expressive programming language, offers a wide range of operators that allow developers to perform various operations like arithmetic, comparison, and logical operations. In this article, we’ll dive into the details of Swift’s operators, exploring their syntax, usage, and key features.
Overview
Section titled “Overview”Swift operators can be broadly categorized into the following groups:
- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Range operators
- Assignment operators
- Custom operators
We’ll examine each category in more detail below.
1. Arithmetic Operators
Section titled “1. Arithmetic Operators”Arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, and division.
1.1 Basic Arithmetic Operators
Section titled “1.1 Basic Arithmetic Operators”- Addition (
+): Adds two numbers. - Subtraction (
-): Subtracts the second number from the first. - Multiplication (
*): Multiplies two numbers. - Division (
/): Divides the first number by the second.
let a = 10let b = 5
let sum = a + b // 15let difference = a - b // 5let product = a * b // 50let quotient = a / b // 21.2 Remainder Operator
Section titled “1.2 Remainder Operator”- Remainder (
%): Calculates the remainder after dividing the first number by the second.
let remainder = a % b // 01.3 Unary Minus Operator
Section titled “1.3 Unary Minus Operator”- Unary minus (
-): Changes the sign of a number.
let c = -a // -101.4 Unary Plus Operator
Section titled “1.4 Unary Plus Operator”- Unary plus (
+): Returns the value unchanged.
let d = +a // 102. Comparison Operators
Section titled “2. Comparison Operators”Comparison operators are used to compare two values and return a boolean result (true or false).
- Equal to (
==): Checks if two values are equal. - Not equal to (
!=): Checks if two values are not equal. - Greater than (
>): Checks if the first value is greater than the second. - Less than (
<): Checks if the first value is less than the second. - Greater than or equal to (
>=): Checks if the first value is greater than or equal to the second. - Less than or equal to (
<=): Checks if the first value is less than or equal to the second.
let e = 10let f = 5
let isEqual = e == f // falselet isNotEqual = e != f // truelet isGreater = e > f // truelet isLess = e < f // falselet isGreaterOrEqual = e >= f // truelet isLessOrEqual = e <= f // false3. Logical Operators
Section titled “3. Logical Operators”Logical operators are used to combine multiple conditions and return a boolean result (true or false).
- Logical NOT (
!): Negates the truth value of a condition. - Logical AND (
&&): Returnstrueif both conditions are true. - Logical OR (
||): Returnstrueif at least one condition is true.
let g = truelet h = false
let notG = !g // falselet andResult = g && h // falselet orResult = g || h // true4. Bitwise Operators
Section titled “4. Bitwise Operators”Bitwise operators perform operations on the individual bits of integer values.
- Bitwise NOT (
~): Flips the bits of a number. - Bitwise AND (
&): Sets each bit to 1 if both bits are 1. - Bitwise OR (
|): Sets each bit to 1 if either bit is 1. - Bitwise XOR (
^): Sets each bit to 1 if the bits are different. - Bitwise Left Shift (
<<): Shifts bits to the left by a specified number of positions. - Bitwise Right Shift (
>>): Shifts bits to the right by a specified number of positions.
let i = 0b1100let j = 0b1010
let bitwiseNot = ~i // 0b0011let bitwiseAnd = i & j // 0b1000let bitwiseOr = i | j // 0b1110let bitwiseXor = i ^ j // 0b0110let leftShift = i << 1 // 0b11000let rightShift = i >> 1 // 0b01105. Range Operators
Section titled “5. Range Operators”Range operators are used to represent a range of values.
- Closed Range Operator (
...): Includes both the start and end values. - Half-Open Range Operator (
..<): Includes the start value but not the end value.
let closedRange = 1...5 // 1, 2, 3, 4, 5let halfOpenRange = 1..<5 // 1, 2, 3, 46. Assignment Operators
Section titled “6. Assignment Operators”Assignment operators are used to assign a value to a variable or constant.
- Simple assignment operator (
=): Assigns a value to a variable. - Compound assignment operators (e.g.,
+=,-=,*=,/=): Performs an operation and assigns the result to a variable.
var k = 10k = 5 // Assigns the value 5 to k
k += 5 // Adds 5 to k and assigns the result to k (k = 10)k -= 3 // Subtracts 3 from k and assigns the result to k (k = 7)k *= 2 // Multiplies k by 2 and assigns the result to k (k = 14)k /= 2 // Divides k by 2 and assigns the result to k (k = 7)7. Custom Operators
Section titled “7. Custom Operators”Swift allows developers to define custom operators with unique symbols and precedence. This can make code more expressive and easier to read.
To create a custom operator, use the operator keyword followed by the operator’s symbol and precedence group.
infix operator **: MultiplicationPrecedence // Declare a custom exponentiation operator
func **(base: Double, exponent: Double) -> Double { return pow(base, exponent)}
let l = 2.0 ** 3.0 // 8Conclusion
Section titled “Conclusion”Swift offers a rich set of operators that make it a powerful and expressive programming language. By understanding how to use these operators effectively, developers can write more efficient and maintainable code. The different types of operators, such as arithmetic, comparison, logical, bitwise, range, assignment, and custom operators, provide a wide range of functionality to perform various operations and manipulate data in Swift.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js