Wednesday 16 November 2016

Properties In Swift

Today we learn about  properties used in swift. Properties associate values with a particular class, structure, or enumeration.There are two types of properties used in swift.

  1. Stored Property
  2. Computed Property
Stored Property : 
Stored properties store constant and variable values as part of an instance.Stored properties are provided only by classes and structures.

Computed Property :
Computed properties calculate a value.Computed properties are provided by classes, structures, and enumerations.

Stored and computed properties are usually associated with instances of a particular type. However, properties can also be associated with the type itself. Such properties are known as type properties.

In addition, you can define property observers to monitor changes in a property’s value, which you can respond to with custom actions. Property observers can be added to stored properties you define yourself, and also to properties that a subclass inherits from its superclass.

Example of Stored Property:

struct Person {let name : String
var fullAddress: String
}
let person = Person(name: "Peter", fullAddress: "House # 981 , Mumbai")

Here name and fullAddress is stored property.name is constant stored property while fullAddress is variable stored property.

Stored Properties of Constant Structure Instances :

If you create an instance of a structure and assign that instance to a constant, you cannot modify the instance’s properties, even if they were declared as variable properties.

struct Person {
  let name: String?
  var fullAddress: String?
}
let obj = Person(name: "peter", fullAddress: "mumbai")
obj.fullAddress = "newdelhi"

Error : Cannot assign to property: 'obj' is a 'let' constant.
This is because struct are value type.You have change let to var.Then only it will work.

Stored Properties of Constant Class Instances : If you create an instance of a class and assign that instance to a constant, you  modify the instance’s properties.
class Person1 {
  let name1: String = "ravi"
  var fullAdderess: String = "mumbai"
}
let person1 = Person1()
person1.fullAdderess = "aiims"
print(person1.fullAdderess)

Output: aiims

Here you can see that we making person1 instance as constant and it allows to change the stored property.This is because classes are reference type .You assign an instance of a reference type to a constant, you can still change that instance’s variable properties.

Computed Property 

Classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly. 

Example :

struct Area {
  var width : Double
  var height : Double
  var totalAreas : Double {
    get {
      return width * height
    }
    set(newAreas){
      let result = sqrt(newAreas)
      width = result
      height = result
      }
}
}
var obj = Area(width: 10.0, height: 20.0)
print("old area->",obj.totalAreas)
obj.totalAreas = 550.0
print("new area->",obj.totalAreas)
print("new width->",obj.width)
print("new height->",obj.height)

Output:
old area-> 200.0
new area-> 550.0
new width-> 23.4520787991171
new height-> 23.4520787991171

Explanation: 

In this example "totalAreas" is declared as computed property.In this computed property we have getter and setter to retrieve and set the property.get property calculate the total area.When use call "obj.totalAreas" its getter block  of code is executed and calculate the total area.Now when we set  obj.totalAreas = 550.0 its setter block of code is executed and calculate the width and height.

Shorthand Setter Declaration In Computed Property:If a computed property’s setter does not define a name for the new value to be set, a default name of newValue is used.

struct Area {
  var width : Double
  var height : Double
  var totalAreas : Double {
    get {
      return width * height
    }
    set{
      let result = sqrt(newValue)
      width = result
      height = result
      }
}
}
Read-Only Computed Properties:
A computed property with a getter but no setter is known as a read-only computed property. A read-only computed property always returns a value, and can be accessed through dot syntax, but cannot be set to a different value.

Example:

struct Area {
  var width : Double
  var height : Double
  var totalAreas : Double {
    get {
      return width * height
    }
 }
}
var obj = Area(width: 10.0, height: 20.0)
print("old area->",obj.totalAreas)

Output:
old area-> 200.0

Note*
You must declare computed properties—including read-only computed properties—as variable properties with the var keyword, because their value is not fixed. The let keyword is only used for constant properties, to indicate that their values cannot be changed once they are set as part of instance initialization.

Conclusion: 
Today we learn about stored and computed properties.
If you have any questions just leave a comment below and I’ll respond as soon as I can.


No comments:

Post a Comment