Javatpoint Logo
Javatpoint Logo

Swift4 Properties

Swift4 properties are used to associate values for class, enumeration, or structure.

Swift4 properties can be classified in 2 types:

  • Stored Properties
  • Computed Properties

Difference between Stored Properties and Computed Properties

Stored Property Computed Property
The stored property stores constant and variable values as instance. The computed property is used to calculate a value rather than storing the value.
The stored property is provided by classes and structures. The computed property is provided by classes, enumerations and structures

Both Stored and Computed properties are associated with instances type. When a property is associated with its type values then it is defined as 'Type Properties'.

Stored and computed properties are usually associated with instances of a particular type. Properties can also be associated with the type itself. Such properties are known as type properties.

Property observers are used to observe the value of the stored properties and also observe the property of inherited subclass derived from superclass.

Stored Property

In Swift4, stored properties of constants are defined by the 'let' keyword and stored properties of variables are defined by the 'var' keyword. Stored property provides 'default value' during definition and it can be initialized and modified by the user during initialization.




Here, the variable pi is initialized as a stored property value with the instance pi = 3.1415. So, whenever the instance is referred, it will hold the value 3.1415.

Lazy Stored Property

Lazy stored property is a flexible property of Swift4. It doesn't calculate the initial values when the variable is initialized for the first time. The 'lazy' modifier is used before the variable declaration to act as a lazy stored property.

Lazy Properties are used:

  • To delay object creation.
  • When the property is dependent on other parts of a class, that are not known yet.




Instance Variables

In Objective-C, stored properties have instance variables for backup and store the value declared in stored property.

Swift 4 incorporates both these concepts into a single 'stored property' declaration. Instead of having a corresponding instance variable and back up value, 'stored property' contains all integrated information defined in a single location about the variables property by variable name, data type and memory management functionalities.

Computed Properties

Computed properties provide a getter and an optional setter to retrieve and set other properties and values indirectly rather than storing it.



(100.0, 50.0)

Computed properties as Property Observer

Swift4 uses property observers to observe and respond to property values. Property observers are called each and every time when property values are set. Except lazy stored properties, we can add property observers to 'inherited' property by method 'overriding'.

You can define Property Observers either:

  • Before storing the value - willset
  • After storing the new value - didset
  • When a property is set in an initializer willset and didset observers cannot be called.



Total Counter is: 100
Newly Added Counter 100
Total Counter is: 500
Newly Added Counter 400

Local Variable vs Global Variables

Local variables and global variables both are used for computing and observing the properties.

Local Variables Global Variables
Local Variables are defined within a function, method, or closure context. Global Variables are defined outside function, method, closure, or type context.
Local Variables are used to store and retrieve values. Global variables are used to store and retrieve values.
Stored properties are used to get and set the values. Stored properties are used to get and set the values.
In local variables, computed properties are used. In global variables, computed properties are also used.

Swift4 Type Properties

Swift4 properties are defined in the type definition section in curly braces {} and scope of the variables are also defined previously. To define type property of value types, ?static? keyword is used and for class types, ?class? keyword is used.


How to set and query type properties?

Swift4 type properties are also set and queried same as instance properties. It uses '.' syntax just on the type alone instead of pointing to the instance.




Next TopicSwift Inheritance

Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA