By David Li on 2023-06-21T05:13:03.000Z

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:

- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Range operators
- Assignment operators
- Custom operators

We’ll examine each category in more detail below.

Arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, and division.

- 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 = 10
let b = 5
let sum = a + b // 15
let difference = a - b // 5
let product = a * b // 50
let quotient = a / b // 2
```

- Remainder (
`%`

): Calculates the remainder after dividing the first number by the second.

`let remainder = a % b // 0`

- Unary minus (
`-`

): Changes the sign of a number.

`let c = -a // -10`

- Unary plus (
`+`

): Returns the value unchanged.

`let d = +a // 10`

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 = 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`

).

- Logical NOT (
`!`

): Negates the truth value of a condition. - Logical AND (
`&&`

): Returns`true`

if both conditions are true. - Logical OR (
`||`

): 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.

- 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 = 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.

- 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, 5
let halfOpenRange = 1..<5 // 1, 2, 3, 4
```

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 = 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.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-05-06.