As I am learning swift I find the concept of Lazy a bit confusing .
Lazy property is initialised when it is needed or accessed by the class instance
class Employee
{
var name : String
lazy var salary = Salary(Basic : 25000 ,HRA : 3000 , DA : 4000)
lazy var minExperience = 0
init(nameValue :String)
{
name = nameValue
} }
var emp1 = Employee("John") // Here the minExperience and
//salary will be nil as they are not assigned any space =
//emp1.salary.storage = nil , emp1.minExperience.storage = nil
// now access the minExperience
emp1.minExperience // using 1st time and this will return 0 ! emp1.salary.HRA // using 1st time and this will return 3000 !
So my questions are :
as
minExperienceandsalaryare not assigned any storage space so till we access it where does it stores it values -0or3000???one aspect of Lazy property is
Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete.
then why Xcode is forcing us to assign value at declaration time ??
- And as we have assigned a value to the Lazy property obviously we don't need to initialise it in our
initmethod .