Operators in Swift

In Swift, operators can be categorized into several groups based on their functionality. This list categorizes all of the Swift operators into their appropriate categories based on their functionality and gives a short description about their function:

Unary Operators

They perform operations on a single operand, such as negating its value or checking its type.

  • + unary plus operator
  • unary minus operator
  • ! logical NOT operator

Arithmetic Operators

They perform basic arithmetic calculations on numeric values, such as addition and multiplication.

  • + addition operator
  • subtraction operator
  • * multiplication operator
  • / division operator
  • % remainder operator

Compound Assignment Operators

They combine an arithmetic operation with an assignment operation, such as adding a value to a variable and assigning the result back to the variable.

  • += addition assignment operator
  • -= subtraction assignment operator
  • *= multiplication assignment operator
  • /= division assignment operator
  • %= remainder assignment operator

Comparison Operators

They compare two values and return a Boolean result indicating whether the comparison is true or false.

  • == equal to operator
  • != not equal to operator
  • < less than operator
  • > greater than operator
  • <= less than or equal to operator
  • >= greater than or equal to operator

Logical Operators

They perform logical operations on Boolean values, such as negating a Boolean value or combining two Boolean values using AND or OR.

  • ! logical NOT operator
  • && logical AND operator
  • || logical OR operator

Range Operators

They create a sequence of values, typically used with loops to iterate over a range of values.

  • closed range operator
  • ..< half-open range operator

Ternary Conditional Operator

This is shorthand way of writing a conditional expression that returns one of two values depending on whether a condition is true or false.

  • a ? b : c ternary conditional operator (Think of the ternary if-then operator)

Nil-Coalescing Operator

It provides a default value to use when an optional value is nil.

  • a ?? b nil-coalescing operator

Type-Casting Operators

It converts a value from one type to another, either by checking the value’s type or by forcing the conversion.

  • as type casting operator
  • as? optional type casting operator
  • as! forced type casting operator

Bitwise Operators

They perform low-level operations on binary data, such as AND, OR, XOR, and bit shifting.

  • & bitwise AND operator
  • | bitwise OR operator
  • ^ bitwise XOR operator
  • ~ bitwise NOT operator
  • << bitwise left shift operator
  • >> bitwise right shift operator

Overflow Operators

They perform arithmetic operations on numeric values, while detecting and handling arithmetic overflow conditions.

  • &+ arithmetic overflow addition operator
  • &- arithmetic overflow subtraction operator
  • &* arithmetic overflow multiplication operator

Identity Operators

They test whether two variables or constants refer to the same object or location in memory.

  • === identity operator
  • !== non-identity operator

Leave a Reply

Your email address will not be published. Required fields are marked *