Advanced Operators

来源:互联网 发布:mac python twisted 编辑:程序博客网 时间:2024/06/03 07:19

/* Advanced Operators -> 更新到XCode Beta51.默认运算是不可以溢出的,否则报错。如果是故意溢出,但是swift提供了一组已&开头的运算符 &+可以溢出。5个可溢出操作 &+ &- &* &/(除0操作返回0) &%var a:UInt8 = 255 &+ 1; //结果为0✨✨var a:UInt8 = 300 &+ 1;这个算式编译会报错,因为swift自动推倒类型后,后面两个加法都是UInt8的,300范围外,会报如下错误:Integer literal overflows when stored int UInt8.(即当把300存到Uint8时出错)2.操作符重载中缀运算符如+-等不需要特殊标示,混合赋值语句 += 也不需要加特殊标示前置++用 prefix 后置++用postfix;注意新版本中去掉了@,=运算符不再需要assignment修饰前置的还有一种情况就是只有一个+或者减号3.自定义新的运算符需要先声明再定义,声明中需要infix,prefix,postfix说明,定义中infix不需要。一元运算符需要指明前置,和后置,括号中的结合性,优先级不需要指定。infix operator +- { associativity left precedence 140 } //声明func +- (l, r){} //定义*///Bitwise operatorlet temp:UInt8 = 0b00001111//NOT 按位取反println("~temp: \(~temp)")//0b11110000//Bitwise And 按位与println(temp & (~temp))//0b00000000//Bitwise OR 按位或println(temp | (~temp))//0b11111111//Bitwise XOR 按位异或  ^//Bitwise Left and Right Shift  Operators <<  >>//无符号数左右移动后补零//有符号数最高位保持不变,其余位左移动时右边补零,右移动时负数补1正数补 0//操作符重载,这里conform Printable协议,可以用println直接输出。struct Vector2D: Printable{    var x = 0.0, y = 0.0        // Printable协议    var description:String {        get {            return "x: \(x), y: \(y)"        }    }}//+func + (l: Vector2D, r: Vector2D) -> Vector2D{    return Vector2D(x: l.x + r.x, y: l.y + r.y)}//-func - (l: Vector2D, r: Vector2D) -> Vector2D{    return Vector2D(x: l.x - r.x, y: l.y - r.y)}//postfix ++ 并且实现赋值功能postfix func ++ (inout v: Vector2D) -> Vector2D{    return Vector2D(x: v.x++, y: v.y++)}//prefix ++prefix func ++ (inout v: Vector2D) -> Vector2D{    return Vector2D(x: ++v.x, y: ++v.y)}//prefix -prefix func - (inout v: Vector2D) -> Vector2D{    return Vector2D(x: -v.x, y: -v.y)}//==func == (l: Vector2D, r: Vector2D) -> Bool{    return ( (l.x == l.x) && (l.y == r.y) ) ? true : false;}//!=func != (l: Vector2D, r: Vector2D) -> Bool{    return ( (l.x == l.x) && (l.y == r.y) ) ? false : true;}func += (inout l:Vector2D, r:Vector2D){    l = l + r//这里可以直接用前面实现的加法运算;a += b; a 是第一个左参数,b是第二个右参数}var v1 = Vector2D(x:1.0, y:3.0)var v2 = Vector2D(x:1.0, y:1.0)println("v1+v2: \(v1+v2)")var v3 = v2 + ++v1println("v2 + ++v1 : \(v3)")println(v1)v1 = Vector2D(x:1.0, y:3.0)v3 = v2 + v1++println(v3)println(v1)//可以自定义符号, = - + * % < > ! & | ^ . ~,开头的符号即可。//新符号定义前需要声明✨✨,声明中需要用infix,prefix,postfix符号infix operator +- { associativity left precedence 140 }//当定义前置和后置运算符时,{}中的内容不可以定义,默认为最高级别的。func +- (l:Vector2D, r: Vector2D) ->Vector2D{    return Vector2D(x: l.x + r.x, y: l.y - r.y)}//定义的时候不需要用infix,声明的时候需要。/*Language Reference->Expression->Binary Expressions的介绍Exponentiative (No associativity, precedence level 160)    << Bitwise left shift    >> Bitwise right shiftMultiplicative (Left associative, precedence level 150)    * Multiply    / Divide    % Remainder    &* Multiply, ignoring overflow    &/ Divide, ignoring overflow    &% Remainder, ignoring overflow    & Bitwise ANDAdditive (Left associative, precedence level 140)    + Add    - Subtract    &+ Add with overflow    &- Subtract with overflow    | Bitwise OR    ^ Bitwise XORRange (No associativity, precedence level 135)    ..< Half-open range    ... Closed rangeCast (No associativity, precedence level 132)    is Type check    as Type castComparative (No associativity, precedence level 130)    < Less than    <= Less than or equal    > Greater than    >= Greater than or equal    == Equal    != Not equal    === Identical    !== Not identical    ~= Pattern matchConjunctive (Left associative, precedence level 120)    && Logical AND    Disjunctive (Left associative, precedence level 110)    || Logical ORNil Coalescing (Right associative, precedence level 110)    ?? Nil coalescingTernary Conditional (Right associative, precedence level 100)    ?: Ternary conditionalAssignment (Right associative, precedence level 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*///《完》


0 0