In this tutorial you will learn about the Swift Properties and its application with practical example.
Swift Properties
Properties is way to associate an attribute and its value with a particular class, structure, or enumeration.Properties can be a constant or variable that stores associated values for classes, structures or enumerations. In swift, properties have getter and setter methods to set, modify or retrieve property value.
In Swift, properties can classified into following two types –
- Stored properties
- Computed properties
Swift Stored Properties
The stored properties are simply a constant or variable that is being associated and stored as part of an instance of any specific class or structure. The stored properties can be a variable or constant stored properties that can be declared using var or let keyword with an initial value.
Example:-
1 2 3 4 5 6 7 8 9 |
struct employee { var empName: String = "John" let empAge: Int } var emp = employee(empName: "Alex", empAge: 35) print("W3Adda - Swift Stored Properties") print("Employee Name: \( emp.empName) ") print("Employee Age: \( emp.empAge) ") |
Output:-
Swift Lazy Stored Properties
In Swift we have a flexible property type called as lazy stored properties whose initial values is not evaluated until it is accessed or used for the first time. Lazy stored properties are useful when we want to hold initialization for some complex computations or when initial value of property to dependent upon some external factors. In swift a property can be declared as lazy stored property using lazy keyword just before its declaration.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class getSum{ lazy var sum = getNum(n1: 10, n2: 15) // 'var' declaration is required. } class getNum{ var n1:Int = 10 var n2:Int = 15 var sum:Int init(n1: Int, n2: Int) { self.n1 = n1 self.n2 = n2 self.sum = n1 + n2 } } var result = getSum() print("W3Adda - Swift Lazy Stored Properties") print(result.sum.sum) |
Output:-
Swift Computed Properties
In swift unlike stored properties computed properties calculates value at the run time rather than to store.Computed properties have a getter to retrieve the value and an optional setter to initialize values.Computed properties can be defined for classes, structures, and enumerations whereas stored properties are defined for classes and structures.
Computed properties can also be declared using var keyword, but rather than an initial value we assign a function to calculate the value.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Circle{ var radius:Double init(radius: Double) { self.radius = radius } var area:Double { return(3.14 * radius * radius) } } var c = Circle(radius: 3) var result = c.area print("W3Adda - Swift Computed Properties") print("Area of circle is: \(result)") |
Output:-
Swift Read-Only Computed Properties
The Read-Only computed property are computed property with only getter but no setter. Read-Only computed property are always mean to return a value and can be accessed through dot (.) syntax.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class mathOps{ var num1 : Int = 5 var num2 : Int = 10 var sum: Int { get { return num1 + num2 } } } let mo = mathOps() var result = mo.sum print("W3Adda - Swift Read Only Computed Properties") print("Sum of two numbers is :\(result)") |
Output:-
Swift Property Observer
In swift property observers observe and responds to changes in a property’s value. Property observer is invoked every time property’s value changes or set to new value, even if the new values is same as current value.Property observers can be added to any stored properties except lazy stored properties, it can also be added to any inherited property by overriding the property within a subclass.
In swift, we have following two types of observers.
- willSet – It is called just before the value is stored.
- didSet – It is called after a new value is stored.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class employee { var empAge: Int = 30 { willSet(newAge){ print("Employee's age is: \(empAge)") if newAge != empAge { print("Setting employee's age to : \(newAge)") } } didSet { if empAge != oldValue { print("Employee's new age is : \(empAge)") } } } } let emp = employee() print("W3Adda - Swift Property Observer") emp.empAge = 35 |
Output:-
Swift Type Property
In Swift, Instance properties are associated with individual instance of a class or structure. Every a new instance created it has separate copy of property values from other instances.In Swift, we can define a special kind of properties that is associated to the class or structure itself, rather than any individual instance no matter how many instances have been created, there will always one copy of these properties will available for all instances of that type.These properties are termed as type properties.Type properties can be defined using ‘static‘ keyword and for class types ‘class‘ keyword is used.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
struct sayHello { static let msg = "Hello, W3Adda!" } class welcomeOne { func printMsg() { print(sayHello.msg) } } class welcomeTwo { func printMsg() { print(sayHello.msg) } } var insOne = welcomeOne() var insTwo = welcomeTwo() insOne.printMsg() insTwo.printMsg() |
Output:-