Stored and Computed Properties in Swift




1, Stored Properties

The simplest form of a variable declaration provides only a type. Stored variables must be initialized before use. As such, an initial value can be provided at the declaration site.

 "A stored property is one that saves a value for use later."

var count : Int



2, Computed Properties

computed variable behaves syntactically like a variable, but does not actually require storage.  compute the value by providing a getter and an optional setter to retrieve and set other properties and values indirectly rather than storing values directly.
get { } : When retrieving the value of a property this block of code will get executed.
set { } : When setting the value of a property this part of code will get executed.
"A computed property is one that runs some code in order to calculate the value."

var variableName: dataType {
    get {
        //code to execute
        return someValue
    }
    set(newValue) {
        //code to execute
    }
}
Thanks for reading, Happy Coding 👨‍💻 

Popular posts from this blog

Frame vs bounds in Swift

DateFomatter in Swift

Property wrapper in Swift