Question

In: Computer Science

In swift language, what is the difference between a stored property, computed property, property observers and...

In swift language, what is the difference between a stored property, computed property, property observers and lazy property?

Solutions

Expert Solution

Usually properties used to store the values. In swift language there are two kinds of property available to store the values.

Stored Property:

A constant or variable is stored as part of the instance. It used variable stored property, constant stored property. In variable stored property achieved by the word var and constant stored property achieved by the word let.

example:

struct Range{

var StartingRange : Int

let length : Int

var range=Range(StartingRange:0,length:3)

//range value starts from 0,1,2

range.StartingRange = 6

//the range starts from 6,7,8

}

In the above example we have used the variable stored property for the StartingRange variable. Hence we can changed in the middle of the program. But we have used constant stored property for the length variable. So that we cannot change the length 3.

Computed Property:

It provide a getter and setter method to set and retrieve the other property and values indirectly.

Example:

class Rectangle{

var length: Double?

var height : Double?

var Area: Double {

get {

return length! * height!

}

set (newArea){

cval = Double(newArea)

}

}

}

let result = Rectangle()

result.Area = 20

print(result.cval)

In the above example it use the get and set method for calculating the area of a Rectangle.

Property Observers:

Property observers observe and respond to changes in a property's value. Property observers are called every time a property's value is set, even if the new value is the same as the property's current value.  Property observers is really used when we want to configure a view based on a given value.

Lazy Property:

In Lazy property,  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. Lazy properties are also useful when the initial value for a property requires complex or computationally expensive setup that should not be performed unless or until it is needed.

Stored Property Computed Property Lazy Property Property Observers
A stored property is a constant or variable that is stored as part of an instance of a particular class or structure In Computed Property 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 In Lazy property,  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. Property observers observe and respond to changes in a property's value.
Whether the variable used or not but the space was allocated for the stored property. In Computed property has both getter and setter to store and retrieve the values In Lazy property closure associated with the lazy property is executed only if you used that property. If for some reason that property is not used you avoid unnecessary allocation and computation. Property observers are called every time a property's value is set, even if the new value is the same as the property's current value.

Related Solutions

What is the difference between real property and intellectual property?
What is the difference between real property and intellectual property?
1) What is the difference between Real Property and Personal Property?
Please Answer the following questions, USA based1) What is the difference between Real Property and Personal Property?2) Name and Explain one Classification of Estates?3) What is a Mechanics Lien?
what is the difference between a rental property and one that is not a rental property...
what is the difference between a rental property and one that is not a rental property (used for personal purposes)?
With respect to the division of property, what is the difference between community property states and...
With respect to the division of property, what is the difference between community property states and states that are not community property states?
what is the key difference between ''property'' and ''non-property'' of a system in thermodynamics?
what is the key difference between ''property'' and ''non-property'' of a system in thermodynamics?
Explain the difference between real property and personal property. What is a fee simple?
 Explain the difference between real property and personal property. What is a fee simple? List and define the other ways in which ownership rights in property can be held.
swift language declare a Swift array variable that can hold instances of any class type
swift language declare a Swift array variable that can hold instances of any class type
What is the difference between Fact and Query in a logic programming language?
What is the difference between Fact and Query in a logic programming language?
What is the difference between real property and personal property? Why does the law make a...
What is the difference between real property and personal property? Why does the law make a distinction between real property and personal property?
In C language: What is the difference between the step and stepi gdb commands? Why is...
In C language: What is the difference between the step and stepi gdb commands? Why is this difference significant?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT