Swift - New Value keyword in computed properties

Introduction

Earlier you used the name position to define the tuple that contains the new position:

class Point{
    var x = 0
    var y = 0
    
    var newPosition:(Double, Double) {
        get {
            return (x, y)
        }
        set  ( position ) {    //position is a tuple
          x =  position .0  //x
          y =  position .1  //y
        }
    }


}

If you did not define a name for the tuple, you can use the shorthand name of newValue , as shown here:


var newPosition:(Double, Double) 
  get 
      return  (x, y)
  set {  //newValue (shorthand name) is a tuple
      x =  newValue.x
      y =  newValue.y
  

Related Topic