Lokang 

Swift and MySQL

Basic operator

In Swift, operators are special symbols or phrases that you use to check, modify, or combine values. Swift supports a variety of operators, which can be broadly categorized as follows:

Arithmetic Operators

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Remainder

Example:

let sum = 5 + 3
let product = 5 * 3

Comparison Operators

  • ==: Equal to
  • !=: Not equal to
  • <: Less than
  • <=: Less than or equal to
  • >: Greater than
  • >=: Greater than or equal to

Example:

if 5 == 5 {
   print("Equal")
}

Logical Operators

  • !: NOT
  • &&: AND
  • ||: OR

Example:

if 5 > 3 && 2 < 4 {
   print("True")
}

Compound Assignment Operators

  • +=: Addition assignment
  • -=: Subtraction assignment
  • *=: Multiplication assignment
  • /=: Division assignment
  • %=: Remainder assignment

Example:

var a = 5
a += 2

Range Operators

  • Closed Range (...)
  • Half-Open Range (..<)

Example:

for i in 1...5 {
   print(i)
}

Nil-Coalescing Operator (??)

Used to provide a default value for optional variables.

Example:

let optionalInt: Int? = nil
let value = optionalInt ?? 0

Ternary Conditional Operator (? :)

Example:

let result = 5 > 3 ? "Yes" : "No"

Identity Operators

  • ===: Referential identity
  • !==: Not identical

Bitwise Operators

  • ~: NOT
  • &: AND
  • |: OR
  • ^: XOR
  • <<: Left Shift
  • >>: Right Shift

Example:

let bitwiseNot = ~5

Assignment Operator

  • =: Assign value

Example:

let b = 5

Member Access Operator

  • .

Example:

let array = [1, 2, 3]
let firstElement = array.first

Optional Chaining

  • ?.: Used to call properties, methods, and subscripts on optional that might currently be nil.

Example:

let optionalArray: [Int]? = [1, 2, 3]
let count = optionalArray?.count

Subscript Operator

  • []

Example:

let array = [1, 2, 3]
let first = array[0]

Function Call Operator

  • ()

Example:

func greet() {
   print("Hello")
}
greet()

Swift also allows you to define custom operators with specific characteristics like associativity, precedence, etc.

This is a broad overview, and Swift has many other specific behaviors and characteristics with these operators. You may also overload operators and create custom ones to suit your specific needs.