Google News
logo
IOS - Interview Questions
What do you mean by lazy property in iOS?
Lazy properties are properties whose initial value isn't computed until the first time they are used. Including the lazy modifier/keyword before the declaration of a stored property indicates it is lazy. This lets you delay the initialization of stored properties. This could be a great way to streamline your code and reduce unnecessary work. When a code is expensive and unlikely to be called consistently, a lazy variable can be a great solution. 
 
Example : 
class Person { 
var name: String
lazy var personalizdgreeting : String = {  
      return “HelloScala \(self.name)!”  
}() 
init(name: String) { 
           self.name = name 
            } 
}
 
As shown above, we aren't sure what value personalizedgreeting should have. To know the correct greeting for this person, we need to wait until this object is initialized.
Advertisement