Page 119

21. Swift – Properties


Swift language provides properties for class, enumeration or structure to associate values. Properties can be further classified into Stored properties and Computed properties. Difference between Stored Properties and Computed Properties

Stored Property

Computed Property

Store constant and variable values as instance

Calculate a value rather than storing the value

Provided by classes and structures

Provided by classes, enumerations and structures

Both Stored and Computed properties are associated with instances type. When the properties are 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. However, properties can also be associated with the type itself. Such properties are known as type properties. Property observers are also used 

To observe the value of the stored properties

To observe the property of inherited subclass derived from superclass

Stored Properties Swift introduces Stored Property concept to store the instances of constants and variables. Stored properties of constants are defined by the 'let' keyword and Stored properties of variables are defined by the 'var' keyword. 

During definition Stored property provides 'default value'

During Initialization the user can initialize and modify the initial values

struct Number { var digits: Int let pi = 3.1415 }

var n = Number(digits: 12345) n.digits = 67 107

​Swift is just a effective and user-friendly programming language for macOS, iOS, watchOS and ...