swift - mutating(杖举或结构体,通过方法来改变自身的值)

//更改了self的值

struct Point2 {

    var x = 0.0 , y = 0.0

    mutating func moveByX (deltaX:Double,y deltaY:Double){

    

        self = Point2(x:x+deltaX,y: y+deltaY)

    }

    

}


//更改了self的值

enum TriStateSwitch {

    case Off,Low,High

    mutating func next(){

    

        switch self {

        case .Off:

            self = Low

        case .Low:

            self = High

        case .High:

            self = Off

        }

    }

}


var ovenLight = TriStateSwitch.Low

ovenLight.next()

ovenLight.next()


你可能感兴趣的:(ios,结构体,杖举,mutating)