Swift Tutorial - Tutorialspoint

(backadmin) #1

}


}


let NewCounter = Samplepgm()

NewCounter.counter = 100
NewCounter.counter = 800

When we run the above program using playground, we get the following result:


Total Counter is: 100
Newly Added Counter 100
Total Counter is: 800
Newly Added Counter 700

Local and Global Variables


Local and global variable are declared for computing and observing the properties.


Local Variables Global Variables

Variables that are defined within a
function, method, or closure context.

Variables that are defined outside function,
method, closure, or type context.

Used to store and retrieve values. Used to store and retrieve values.

Stored properties is used to get and set
the values.

Stored properties is used to get and set the
values.

Computed properties are also used. Computed properties are also used.

Type Properties


Properties are defined in the Type definition section with curly braces {} and scope of the
variables are also defined previously. For defining type properties for value types 'static'
keyword is used and for class types 'class' keyword is used.


Syntax


struct Structname {
static var storedTypeProperty = " "
static var computedTypeProperty: Int {
// return an Int value here
}
}
Free download pdf