Updated 28 December 2020
Let first see Stored Properties, its main role is to store constant and variable as a part of the instance. It is only provided by classes and structure. At the time of initialization, you can set and modify the initial values.
Let see an example of it using structure.
1 2 3 4 5 6 7 |
struct Box{ let width:Int var height:Int } var newBox = Box(width:100, height: 120) newBox.height = 140 |
In the above example, the width is constant stored property and height is variable stored property.
Let see one more example
1 2 3 4 5 6 7 |
struct Box{ let width:Int var height:Int } let newBox = Box(width:100, height: 120) newBox.height = 140 |
In this above example newBox.Height gives an error. Because we have created an instance and assigned it to a constant(let newBox).
This is because the structure is of the value type. This will not give an error in the case of class because the class is a reference type.
It is a stored property whose default value is not initialized until it is used for the first time. it is indicated by the “lazy” keyword. Also, it must be declared as a variable, because constant properties have value before the initialization completes.
Let see an example of it.
1 2 3 4 5 6 7 8 9 10 11 12 |
Class SomeFile{ let fileFirst = "File First" var secondFile = "Second File" } Class TestClass{ lazy var someFile = SomeFile() var name = "Test" } let testClassObj = TestClass() Print(testClassObj.name) |
In this above example “someFile” is declared as lazy. So it is not yet initialized unless it is called, Although the Testclass is initialized.
Computed properties provide the getter and optional setter. Also, it does not store the value. It is defined by Class, Struct, and enum.
Let see an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Test{ var firstNumer = 10 var secondNumber = 20 var add:(Int,Int){ get{ return (firstNumer+secondNumber,firstNumer) } set(number){ firstNumer = number.0 secondNumber = number.1 } } } let result = Test() print(result.add)// (30,10) result.add = (5,5) print(result.add)// (10,5) |
If the name of the setter is not defined for the new value, then the default “newValue” is used.
Let see one example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Test{ var firstNumer = 10 var secondNumber = 20 var add:(Int,Int){ get{ return (firstNumer+secondNumber,firstNumer) } set{ firstNumer = newValue.0 secondNumber = newValue.1 } } } let result = Test() print(result.add)// (30,10) result.add = (5,5) print(result.add)// (10,5) |
In this, there is only a getter and not a setter
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
class Test{ var firstNumber = 5 var secondNumber = 10 var add:(Int){ return (firstNumer+secondNumber) } } let result = Test() print(result.add)// 15 |
It responds to property value changes. It is called every time when the property value set.
You can set either one or both observers to the property.
i) wiilSet => it is called when the value is just to store.
ii) didSet => It is called immediately when the value is stored.
Let see an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Account{ var amount: Int = 0{ willSet{ print("Amount is to be set \(newValue)") } didSet{ if amount>oldValue{ print("Total amount added \(amount-oldValue)") } } } } let account = Account() account.amount = 20 //Amount is to be set 20 Total amount added 20 account.amount = 100 //Amount is to be set 100 Total amount added 80 account.amount = 200 //Amount is to be set 200 Total amount added 100 |
Thanks for reading this blog. I hope you will get the basic idea of property.
You can also check other blogs from here. if you have any issue or suggestion you can leave your query/suggestion in the comment section.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.