Swift 自定義運算符

結合性        associativity
可取值範圍    left,right和none

優先級        precedence
可取值範圍    0~255

系統內置運算符結合性質及優先級
    求冪相關(無結合,優先級160)
        << 按位左移(Bitwise left shift)
        >> 按位右移(Bitwise right shift)
 
    乘除法相關(左結合,優先級150)
        * 乘
        / 除
        % 求餘
        &* 乘法,忽略溢出( Multiply, ignoring overflow)
        &/ 除法,忽略溢出(Divide, ignoring overflow)
        &% 求餘, 忽略溢出( Remainder, ignoring overflow)
        & 位與( Bitwise AND)
 
    加減法相關(左結合, 優先級140)
        + 加
        - 減
        &+ Add with overflow
        &- Subtract with overflow
        | 按位或(Bitwise OR )
        ^ 按位異或(Bitwise XOR)
 
    Range (無結合,優先級 135)
        .. 半閉值域 Half-closed range
        ... 全閉值域 Closed range
 
    類型轉換 (無結合,優先級 132)
        is 類型檢查( type check)
        as 類型轉換( type cast)
        <= 小於等於
        >大於
        >= 大於等於
        == 等於
        != 不等
        === 恆等於
        !== 不恆等
        ~= 模式匹配( Pattern match)
 
    合取( Conjunctive) (左結合,優先級 120)
        && 邏輯與(Logical AND)
 
    析取(Disjunctive) (左結合,優先級 110)
        || 邏輯或( Logical OR)
 
    三元條件(Ternary Conditional )(右結合,優先級 100)
        ?: 三元條件 Ternary conditional
 
    賦值 (Assignment) (右結合, 優先級 90)
        = 賦值(Assign)
        *= Multiply and assign
        /= Divide and assign
        %= Remainder and assign
        += Add and assign
        -= Subtract and assign
        <<= Left bit shift and assign
        = Right bit shift and assign
        &= Bitwise AND and assign
        ^= Bitwise XOR and assign
        |= Bitwise OR and assign
        &&= Logical AND and assign
        ||= Logical OR and assign
相關文章
相關標籤/搜索