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.
Swift operators can be broadly categorized into the following groups:
We’ll examine each category in more detail below.
Arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, and division.
+
): Adds two numbers.-
): Subtracts the second number from the first.*
): Multiplies two numbers./
): Divides the first number by the second.let a = 10
let b = 5
let sum = a + b // 15
let difference = a - b // 5
let product = a * b // 50
let quotient = a / b // 2
%
): Calculates the remainder after dividing the first number by the second.let remainder = a % b // 0
-
): Changes the sign of a number.let c = -a // -10
+
): Returns the value unchanged.let d = +a // 10
Comparison operators are used to compare two values and return a boolean result (true
or false
).
==
): Checks if two values are equal.!=
): Checks if two values are not equal.>
): Checks if the first value is greater than the second.<
): Checks if the first value is less than the second.>=
): Checks if the first value is greater than or equal to the second.<=
): Checks if the first value is less than or equal to the second.let e = 10
let f = 5
let isEqual = e == f // false
let isNotEqual = e != f // true
let isGreater = e > f // true
let isLess = e < f // false
let isGreaterOrEqual = e >= f // true
let isLessOrEqual = e <= f // false
Logical operators are used to combine multiple conditions and return a boolean result (true
or false
).
!
): Negates the truth value of a condition.&&
): Returns true
if both conditions are true.||
): Returns true
if at least one condition is true.let g = true
let h = false
let notG = !g // false
let andResult = g && h // false
let orResult = g || h // true
Bitwise operators perform operations on the individual bits of integer values.
~
): Flips the bits of a number.&
): Sets each bit to 1 if both bits are 1.|
): Sets each bit to 1 if either bit is 1.^
): Sets each bit to 1 if the bits are different.<<
): Shifts bits to the left by a specified number of positions.>>
): Shifts bits to the right by a specified number of positions.let i = 0b1100
let j = 0b1010
let bitwiseNot = ~i // 0b0011
let bitwiseAnd = i & j // 0b1000
let bitwiseOr = i | j // 0b1110
let bitwiseXor = i ^ j // 0b0110
let leftShift = i << 1 // 0b11000
let rightShift = i >> 1 // 0b0110
Range operators are used to represent a range of values.
...
): Includes both the start and end values...<
): Includes the start value but not the end value.let closedRange = 1...5 // 1, 2, 3, 4, 5
let halfOpenRange = 1..<5 // 1, 2, 3, 4
Assignment operators are used to assign a value to a variable or constant.
=
): Assigns a value to a variable.+=
, -=
, *=
, /=
): Performs an operation and assigns the result to a variable.var k = 10
k = 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)
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 // 8
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.