Computed properties

Computed properties are properties that do not have backend variables that are used to store the values associated with the property. The values of a computed property are usually computed when code requests it. You can think of a computed property as a function disguised as a property. Let's look at how we would define a read-only computed property:

var salaryWeek: Double {  
  get{ 
    return self.salaryYear/52 
  } 
} 

To create a read-only computed property, we begin by defining it as if it were a normal variable with the var keyword, followed by the variable name, colon, and the variable type. What comes next is different; we add a curly bracket at the end of the declaration and then define a getter method that is called when the value of our computed property is requested. In this example, the getter method divides the current value of the salaryYear property by 52 to get the employee's weekly salary.

We can simplify the definition of the read-only computed property by removing the get keyword, as shown in the following example:

var salaryWeek: Double { 
  return self.salaryYear/52 
} 

Computed properties are not limited to being read-only, we can also write to them. To enable the salaryWeek property to be writeable, we will add a setter method. The following example shows how we add a setter method that will set the salaryYear property, based on the value being passed into the salaryWeek property:

var salaryWeek: Double {  
  get { 
    return self.salaryYear/52 
  } 
  set (newSalaryWeek){ 
    self.salaryYear = newSalaryWeek*52 
  } 
} 

We can simplify the setter definition by not defining a name for the new value. In this case, the value will be assigned to a default variable named newValue, as shown in the following example.

var salaryWeek: Double {  
  get{ 
    return self.salaryYear/52 
  } 
  set{ 
    self.salaryYear = newValue*52 
  } 
} 

The salaryWeek computed property, as written in the preceding examples, could be added to either the EmployeeClass class or the EmployeeStruct structure without any modifications. Let's see how we can do this by adding the salaryWeek property to our EmployeeClass class:

public class EmployeeClass {  
  var firstName = "" 
  var lastName = ""  
  var salaryYear = 0.0 
 
  var salaryWeek: Double {  
    get{ 
      return self.salaryYear/52 
    } 
    set (newSalaryWeek){ 
      self.salaryYear = newSalaryWeek*52 
    } 
  } 
} 

Now, let's look at how we can add the salaryWeek computed property to the EmployeeStruct structure:

struct EmployeeStruct {  
  var firstName = ""  
  var lastName = ""  
  var salaryYear = 0.0 
 
  var salaryWeek: Double {  
    get{ 
      return self.salaryYear/52 
    } 
    set (newSalaryWeek){ 
      self.salaryYear = newSalaryWeek*52 
    } 
  } 
} 

As we can see, the class and structure definitions are the same so far, except for the initial class or struct keywords that are used to define them.

We read and write to a computed property exactly as we would to a stored property. Code that is external to the class or structure should not be aware that the property is a computed property. Let's see this in action by creating an instance of the EmployeeStruct structure:

var f = EmployeeStruct(firstName: "Jon", lastName: "Hoffman", salaryYear: 39_000) 
print(f.salaryWeek) //prints 750.00 to the console  
f.salaryWeek = 1000 
print(f.salaryWeek) //prints 1000.00 to the console  
print(f.salaryYear) //prints 52000.00 to the console 

The preceding example starts off by creating an instance of the EmployStruct structure with the salaryYear value being set to 39,000. Next, we print the value of the salaryWeek property to the console. This value is currently 750.00. We then set the salaryWeek property to 1,000.00 and print out both the salaryWeek and salaryYear properties to the console. The values of the salaryWeek and salaryYear properties are now 1,000.00 and 52,000 respectively. As we can see, in this example, setting either the salaryWeek or salaryYear property changes the values returned by both.

Computed properties can be very useful for offering different views of the same data. For example, if we had a value that represented the length of something, we could store the length in centimeters and then use computed properties that calculate the values for meters, millimeters, and kilometers.

Now, let's look at property observers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset