Updated 24 December 2019
Properties associate values with a particular class, structure, or enumeration.
Swift Properties are broadly classified into two types.
var
keyword) or constant stored properties (introduced by the let
keyword).Note: A computed property can’t be a constant.
1 2 3 4 5 6 7 8 9 |
struct Student{ let name: String var id: Int } var obj = Rect(name: "John", id: 5) obj.id = 5 |
Both the name and id are stored properties (constant and variable respectively) in the above snippet.
A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy
modifier before its declaration.
NOTE: You must always declare a lazy property as a variable (with the var
keyword), because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.
1 2 3 4 5 6 7 8 |
class TestClass { lazy var testString: String = "Test" } let testClass = TestClass() print(testClass.testString) //TestString |
The property will not initalized until it gets accessed. In this example this is not very obivious though. But since the initialization can also happen inside a block, we can make it a little bit more obvious:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class TestClass { lazy var testString: String = { print("initialize the property") return "Test" }() } let testClass = TestClass() print("before first call") print(testClass.testString) Output is: before first call initialize the property TestString |
In addition to stored properties, 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
struct Rect{ var length : Double let breadth : Double var area : Double { get{ return length*breadth } set(newArea) { length = newArea/breadth } } } var r = Rect(length: 6, breadth: 5) print(r.area) //prints 30.0 r.area = 40 print(r.length) //prints 8.0 |
In the above code getters and setters are used as get { } and set(param_name){ } on the computed property area. The getters and setters are accessed using the dot syntax. If a param name isn’t specified inside the setter, Swift assigns the default name as newValue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
struct Rect{ var length : Double let breadth : Double var area : Double { get{ return length*breadth } set { length = newValue/breadth } } } |
Computed Properties can’t be assigned as a lazy var property. Computed Properties that have a get and set defined can’t be set as a constant let.
Swift Property Observers respond to changes in the property value. These are typically used when two property values are dependent on each other. They contain two methods:
newValue
oldValue
Property Observers get triggered every time the value is set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import UIKit struct Person { var name: String var age: Int } class ViewController: UIViewController { var person: Person? { didSet{ print("Called after setting the new value") if let name = person?.name { print("New name is \(name) and old name is \(String(describing: oldValue?.name))") } } willSet(myNewValue) { print("Called before setting the new value") if let newName = myNewValue?.name { print("New name is \(newName)") } } } override func viewDidLoad() { super.viewDidLoad() person = Person(name: "webkul", age: 4) } } Output:- Called before setting the new value New name is webkul Called after setting the new value New name is webkul and old name is nil |
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.