swift

Properties associate a particular value to classes, structures, and enumerations. Properties can be of two types:

  • stored properties
  • computed properties

The first are constants or variables defined by the keywords let and var and can be part of classes and structures. The second are properties obtained from a calculation and can be part of classes, structures, and enumerations.

Properties, both stored and computed, can be static in this case they are called type properties (see them in the second part).

We are going to define a structure with two stored properties, one variable and the other constant:

struct FixedInterval {

    var startValue: Int

    let interval: Int

}

var intervalThree = FixedInterval(startValue: 0, interval: 3)

intervalThree.startValue = 6

When the instance of the FixedInterval structure is created, two values are associated with the properties. Only one of these two values can be edited afterwards because it is variable, the value defined by let is constant and once initialized it is no longer editable. 

It is necessary to make a clarification about variables and constants. If the FixedInterval structure is associated with a constant, the internally defined variable is also constant. This is because the structure is a value type. The same thing would not be true for a class because the class is a reference type.

In addition to the stored properties where, once the instance is created, values are stored and associated with properties whether they are variable or constant, there are computed properties where values are not stored but obtained through a calculation. These properties use the get and set keyword.

With get, I read the value of the computed property that is a value obtained from the calculation made on other properties.

With set, I write the value in the computed property that is equivalent to writing the value of the stored properties indirectly through the calculation.

In the following example, I set two points on the plane, by means of computed properties I return the distance and the travel speed from point A to point B.

// x and y coordinates at the time t (meters and seconds)

struct PointPlane {

    var x = 0, y = 0

    var t = 0

}

struct Route {

    var start = PointPlane()

    var arrival = PointPlane()

  

    var timeTravel: Int {

        

        get {

            return arrival.t - start.t

        }

        

        set(newT) {

            start.t = 0

            arrival.t = newT

        }

    }

    

    var distance: Double {

        

        get {

            let dX = arrival.x - start.x

            let dY = arrival.y - start.y

            let tmp = sqrt(pow(Double(dX), 2) + pow(Double(dY), 2))

            return tmp

        }

    }

    

    var speed: Double {

        

        get {

            return distance/Double(timeTravel)

        }

    }

}

let pointA = PointPlane(x: 0, y: 0, t: 0)    //I'm setting the starting point

let pointB = PointPlane(x: 10, y: 10, t: 10//I'm setting the arrival point

var routeAB = Route(start: pointA, arrival: pointB)

routeAB.timeTravel = //I'm changing time for travel

let distance = routeAB.distance//I'm reading the computed property = 14,14 m

let speed = routeAB.speed//I'm reading the computed property = 1,76 m/sec

In the structure Route, I've created two stored properties by creating the start and arrival variables that are two instances of the PointPlane structure. Then I've created the computed property timeTravel variable. This variable, when I read it, calculates the difference between the start and arrival properties and returns an integer value. This is done through the get keyword. Through set I can set a new travel time by setting the value t of the start and arrival variables. Two other calculated properties are created: distance and speed; the method is the same, by means of a calculation and the keyword get the variable assumes a precise value when interrogated.

Note that the set keyword is followed by the name of a variable (newT) that is used to convey the new value in the calculation; you can avoid specifying it and Swift assigns as default name "newValue" that can be used instead of newT.

As usual in the perspective of simplification, the two properties of distance and speed are read-only because of without the "setter"; it is therefore possible to avoid writing get and making the code easier to read.

var speed: Double {

  return distance/Double(timeTravel)

}