Lazy var in Swift
As a developer we know that memory management is one of the major concerns when we are developing complex applications. High memory usage can affect our apps performance and can be a major cause of rejection during submission on the App store.
2. You can’t use lazy with computed property, because of a computed property returns the value every time we try to access it after executing the code inside the computation block.
3. You can use lazy with only member of the class and struct.
4. Lazy property is not initialised automatically so its not thread safe.
Thats all for Today. I hope, you find something useful for yourself. If you have any comments, questions, or corrections, feel free to contact me :).
So for dealing with this we can use term lazy with our stored property.
What is lazy ?
A lazy stored property is a property whose initial value is not calculated until the first time it is used.
Now let's go straight to the example.
1. First open your Xcode and create a Single View Application.
2. Now, on your ViewController.swift file create a struct with name MemoryUsage and add any func to it.
3. Create one object of this structure with stored property and another object with lazy stored property to check the memory allocation in View Controller class.
Add breakpoint in ViewDidLoad and check the debugger
1. First open your Xcode and create a Single View Application.
2. Now, on your ViewController.swift file create a struct with name MemoryUsage and add any func to it.
3. Create one object of this structure with stored property and another object with lazy stored property to check the memory allocation in View Controller class.
Add breakpoint in ViewDidLoad and check the debugger
Lazy rules
1. You cant use lazy with let keyword. You must declare a lazy property as a variable (var) because its initial value might not be retrieved until after instance initialisation completes.2. You can’t use lazy with computed property, because of a computed property returns the value every time we try to access it after executing the code inside the computation block.
3. You can use lazy with only member of the class and struct.
4. Lazy property is not initialised automatically so its not thread safe.
Thats all for Today. I hope, you find something useful for yourself. If you have any comments, questions, or corrections, feel free to contact me :).
Comments
Post a Comment