Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, operators are special symbols or phrases that you use to check, change, or combine values. Swift provides a rich set of operators, including arithmetic, comparison, logical, and more. Here's a breakdown of the most commonly used operators in Swift:
Addition (+): Adds two values together.
let sum = 3 + 2 // sum is 5
Subtraction (-): Subtracts one value from another.
let difference = 5 - 3 // difference is 2
Multiplication (*): Multiplies two values.
let product = 3 * 2 // product is 6
Division (/): Divides one value by another.
let quotient = 6 / 3 // quotient is 2
Remainder Operator (%): Determines the remainder when dividing one value by another.
let remainder = 7 % 3 // remainder is 1
Equal (==): Checks if two values are equal.
Not Equal (!=): Checks if two values are not equal.
Greater Than (>): Checks if the left value is greater than the right value.
Less Than (<): Checks if the left value is less than the right value.
Greater Than or Equal (>=): Checks if the left value is greater than or equal to the right value.
Less Than or Equal (<=): Checks if the left value is less than or equal to the right value.
Logical NOT (!): Inverts a Boolean value.
let isFalse = !true // isFalse is false
Logical AND (&&): Combines two Boolean values and returns true
if both values are true.
Logical OR (||): Combines two Boolean values and returns true
if at least one value is true.
A shorthand for a simple if-else
statement.
let value = true ? "It's true" : "It's false"
Closed Range (a...b): Defines a range from a
to b
, and includes both a
and b
.
Half-Open Range (a..<b): Defines a range from a
to b
, but doesn't include b
.
Assigns the value on the right to the variable on the left.
let x = 10
These operators combine assignment with another operation. For example, x += 1
is equivalent to x = x + 1
.
Unwraps an optional if it contains a value, or returns a default value if the optional is nil
.
let optionalInt: Int? = nil let result = optionalInt ?? 5 // result is 5
Used to check if two object references refer to the same instance.
class MyClass {} let objA = MyClass() let objB = MyClass() if objA === objB { // This won't execute because objA and objB are two different instances. }
Operate on the individual bits of numbers.
Swift also supports custom operators, which can be defined and given a specific precedence and associativity. Always be cautious when using or defining custom operators, as they can make the code harder to read if not used judiciously.
Arithmetic operators in Swift:
Description: Arithmetic operators perform basic mathematical operations.
let a = 5 let b = 2 let sum = a + b let difference = a - b let product = a * b let quotient = a / b let remainder = a % b
Comparison operators in Swift:
Description: Comparison operators are used to compare values.
let x = 5 let y = 8 let isEqual = x == y let isNotEqual = x != y let isGreater = x > y let isLess = x < y let isGreaterOrEqual = x >= y let isLessOrEqual = x <= y
Logical operators in Swift:
Description: Logical operators perform logical operations.
let isTrue = true let isFalse = false let andResult = isTrue && isFalse let orResult = isTrue || isFalse let notResult = !isTrue
Swift assignment operators:
Description: Assignment operators are used to assign values to variables.
var x = 5 let y = 8 x += y // Equivalent to: x = x + y x -= y // Equivalent to: x = x - y x *= y // Equivalent to: x = x * y x /= y // Equivalent to: x = x / y
Bitwise operators in Swift:
Description: Bitwise operators perform operations at the bit level.
let a: UInt8 = 0b1010 let b: UInt8 = 0b1100 let andResult = a & b let orResult = a | b let xorResult = a ^ b let notResult = ~a
Swift range operators:
Description: Range operators are used to represent ranges.
// Closed range operator let closedRange = 1...5 // Half-open range operator let halfOpenRange = 1..<5
Custom operators in Swift:
Description: You can define custom operators to perform specific operations.
infix operator ** : MultiplicationPrecedence func **(base: Double, power: Double) -> Double { return pow(base, power) } let result = 2.0 ** 3.0 // Equivalent to pow(2.0, 3.0)
Operator precedence in Swift:
Description: Operator precedence determines the order in which operators are evaluated.
let result = 2 + 3 * 4 // Multiplication has higher precedence than addition
Swift follows standard precedence rules, but you can use parentheses to override the default order.