What are the precedence levels of the Swift operators?

2 Solutions Collect From Internet About “What are the precedence levels of the Swift operators?”

Updated for Swift 3.0

Swift 3.0

There is now a page in the Swift docs.

All the precedence groups are listed below (also visible Command clicking import Swift):

precedencegroup BitwiseShiftPrecedence {
    higherThan: MultiplicationPrecedence
}

precedencegroup FunctionArrowPrecedence {
    associativity: right
}

precedencegroup AssignmentPrecedence {
    associativity: right
    assignment: true
    higherThan: FunctionArrowPrecedence
}

precedencegroup RangeFormationPrecedence {
    higherThan: CastingPrecedence
}

precedencegroup CastingPrecedence {
    higherThan: NilCoalescingPrecedence
}

precedencegroup AdditionPrecedence {
    associativity: left
    higherThan: RangeFormationPrecedence
}

precedencegroup NilCoalescingPrecedence {
    associativity: right
    higherThan: ComparisonPrecedence
}

precedencegroup ComparisonPrecedence {
    higherThan: LogicalConjunctionPrecedence
}

precedencegroup LogicalConjunctionPrecedence {
    associativity: left
    higherThan: LogicalDisjunctionPrecedence
}

precedencegroup LogicalDisjunctionPrecedence {
    associativity: left
    higherThan: TernaryPrecedence
}

precedencegroup DefaultPrecedence {
    higherThan: TernaryPrecedence
}

precedencegroup TernaryPrecedence {
    associativity: right
    higherThan: AssignmentPrecedence
}

precedencegroup MultiplicationPrecedence {
    associativity: left
    higherThan: AdditionPrecedence
}

Swift 2.1

In the Swift header:

infix operator %= {
    associativity right
    precedence 90
    assignment
}

infix operator >= {
    associativity none
    precedence 130
}

infix operator != {
    associativity none
    precedence 130
}

infix operator <<= {
    associativity right
    precedence 90
    assignment
}

infix operator || {
    associativity left
    precedence 110
}

infix operator += {
    associativity right
    precedence 90
    assignment
}

infix operator / {
    associativity left
    precedence 150
}

infix operator - {
    associativity left
    precedence 140
}

infix operator << {
    associativity none
    precedence 160
}

infix operator |= {
    associativity right
    precedence 90
    assignment
}

infix operator >>= {
    associativity right
    precedence 90
    assignment
}

infix operator <= {
    associativity none
    precedence 130
}

infix operator -= {
    associativity right
    precedence 90
    assignment
}

infix operator ?? {
    associativity right
    precedence 131
}

infix operator > {
    associativity none
    precedence 130
}

infix operator ~= {
    associativity none
    precedence 130
}

infix operator + {
    associativity left
    precedence 140
}

infix operator ^ {
    associativity left
    precedence 140
}

infix operator ... {
    associativity none
    precedence 135
}

infix operator /= {
    associativity right
    precedence 90
    assignment
}

infix operator * {
    associativity left
    precedence 150
}

infix operator *= {
    associativity right
    precedence 90
    assignment
}

infix operator & {
    associativity left
    precedence 150
}

infix operator !== {
    associativity none
    precedence 130
}

infix operator % {
    associativity left
    precedence 150
}

infix operator && {
    associativity left
    precedence 120
}

infix operator &* {
    associativity left
    precedence 150
}

infix operator &+ {
    associativity left
    precedence 140
}

infix operator &= {
    associativity right
    precedence 90
    assignment
}

infix operator >> {
    associativity none
    precedence 160
}

infix operator &- {
    associativity left
    precedence 140
}

infix operator === {
    associativity none
    precedence 130
}

infix operator ~> {
    associativity left
    precedence 255
}

infix operator ..< {
    associativity none
    precedence 135
}

infix operator < {
    associativity none
    precedence 130
}

infix operator == {
    associativity none
    precedence 130
}

infix operator ^= {
    associativity right
    precedence 90
    assignment
}

infix operator | {
    associativity left
    precedence 140
}
  1. At the top of your code, where the import statement is, add a new line:

    import Swift
    
  2. Command-click “Swift”.

    Whoa, what’s this? It’s the Swift header file! All of Swift is here – including all the operators and their precedence settings.

  3. Read and learn.