Swift运算符的优先级是什么?

时间:2015-02-04 18:27:58

标签: swift

我想查找某些运算符的优先级。是否有某个列表?我还没找到。

2 个答案:

答案 0 :(得分:11)

  

针对Swift 3.0进行了更新

Swift 3.0

Swift文档中现在有page

下面列出了所有优先级组(同样可见命令单击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

在Swift标题中:

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 :(得分:9)

  1. 在代码的顶部,import语句所在的位置,添加一个新行:

    import Swift
    
  2. 按住Command键并单击“Swift”。

    哇,这是什么?这是Swift头文件!所有Swift都在这里 - 包括所有运算符及其优先级设置。

  3. 阅读和学习。