Swift Tutorial - Tutorialspoint

(backadmin) #1
println("\(n.digits)")
println("\(n.pi)")

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


67


3.1415


Consider the following line in the above code:


let pi = 3.1415

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 alone.


Another method to have stored property is to have as constant structures. So the whole
instance of the structures will be considered as 'Stored Properties of Constants'.


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

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

println("\(n.digits)")
println("\(n.numbers)")
n.numbers = 8.7

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


error: cannot assign to 'numbers' in 'n'
n.numbers = 8.7

Instead of reinitializing the 'number' to 8.7 it will return an error message indicating that
the 'number' is declared as constant.

Free download pdf