SoFunction
Updated on 2025-04-11

A brief analysis of the usage method of Swift operator

Overflow operator

1. When Swift's arithmetic operator overflows, a runtime error will be thrown.

var v: UInt8 = 
v -= 1

2. Swift has overflow operators (&+, &-, &*), used to support overflow operations

var v1 = 
var v2 = v1 &- 1 //v2 = 255
var v1 = 
var v2 = v1 &* 2 // Equivalent to v1 &+ v1

Operator Overload

1. Classes, structures, and enums can provide customized implementations for existing operators. This operation is called: operator overloading.

struct Point {
    var x = 0, y = 0
    static func + (p1: Point, p2: Point) -> Point {
        Point(x:  + , y:  + )
    }
    static prefix func - (p: Point) -> Point {
        Point(x: -, y: -)
    }
    static func += (p1: inout Point, p2: Point) {
        p1 = p1 + p2
    }
    static postfix func ++ (p: inout Point) -> Point {
        let tmp = p
        p += Point(x: 1, y: 1)
        return tmp
    }
    static prefix func ++ (p: inout Point) -> Point {
        p += Point(x: 1, y: 1)
        return p
    }
}
var p1 = Point(x: 10, y: 20)
var p2 = Point(x: 11, y: 22)
let p3 = p1 + p2
let p4 = -p3
print(p4)

Equatable

1. To know whether the two instances are equivalent, the general practice is to abide by the Equatable protocol and overload the == operator

At the same time, it is equivalent to overloading the != operator

class Person: Equatable {
    var age: Int
    init(age: Int) {
         = age
    }
    static func == (lhs: Person, rhs: Person) -> Bool {
         == 
    }
}
var p1 = (age: 10)
var p2 = Person(age: 11)
print(p1 == p2)

2. Swift provides default Equatable implementation for the following types

Enumerations with no associated type

Only have enumerations that comply with the Equatable protocol association type

Only have structures that comply with the Equatable protocol storage properties

enum Answer {
    case wrong
    case right
}
var s1 = 
var s2 = 
print(s1 == s2)

3. Reference type compares whether the stored address values ​​are equal (whether the same object is referenced), and uses the identity operator === , !==

Comparable

1. To compare the sizes of two instances, the general practice is: comply with the Comparable protocol and overload the corresponding operators.

Custom Operator

1. You can customize new operators: use operator to declare them in the global scope

prefix operator prefix operator
postfix operator suffix operator
infix operator infix operator: priority group

precedencegroup Priority Group {
    associativity: Bonding(left/right/none)
    higherThan: Who has the higher priority
    lowerThan: Who has the lower priority
    assignment: trueIt means that there is the same priority as the assignment operator in optional chain operations.
}
prefix operator +++
prefix func +++ (_ i: inout Int) {
    i += 2
}
var age = 10
+++age
infix operator +-: PlusMinusPrecedence
precedencegroup PlusMinusPrecedence {
    associativity: none
    higherThan: AdditionPrecedence
    lowerThan: MultiplicationPrecedence
    assignment: true
}
struct Point {
    var x = 0, y = 0
 
    static func +- (p1: Point, p2: Point) -> Point {
        Point(x:  + , y:  - )
    }
}
class Person {
    var age = 0
    var point: Point = Point()
}
var p: Person? = Person()
p?.point +- Point(x: 10, y: 20)

This is the end of this article about the brief analysis of the usage methods of Swift operators. For more related content of Swift operators, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!