You add stored properties to a class by declaring them just as you would normal variables and constants:
class Point { var x = 0.0 //variable var y = 0.0 //variable let width = 2 //constant }
Here, the code adds two variables to the Point - x , y and a constant, width of Int type.
In Swift, constants and variables that are stored within a class are known as stored properties.
Like structures, stored properties can have default values.
To access the stored properties of a class, you use dot notation . to access an individual property, as shown here:
class Point { var x = 0.0 //variable var y = 0.0 //variable let width = 2 //constant } var ptA = Point() //assigning values to properties ptA.x = 25.0/*from w w w. ja v a2 s .co m*/ ptA.y = 50.0 //retrieving values from properties print(ptA.x) //25.0 print(ptA.y) //50.0 print(ptA.width) //2
Structures support stored properties.