Properties associate values with a particular class, structure, or enumeration.
Types:
1. stored properties: store constant and variable values as part of instance
2.computed properties: calculate value.(not store value )
Enumeration has only computed properties. Classes and structure have both.
For stored properties can have KVO like objective C.
####Stored Properties
Its a constant or variable which stores the value which is part of class or structures. we can provide default values as part of defination.
During initialization of class / structure we can change the value of constant too.
We can not change the stored property value of constant structure instance (becoz structure is value type) but we can change stored property value of constant class instance.
i.e When instance of value type (int, string, float , array etc) marked as constant so all of its properties also becomes constant.
Lazy Stored Properties
is a property whose value is not calculated until it is used .(on first time use its calculated). keyword lazy. lazy properties must be declared with var keyword becoz const (let) must be have value before initialization completes.
Lazy property when value available after initialization or when require more computing.
like objective c A Swift property does not have a corresponding instance variable, and the backing store for a property is not accessed directly.
####Computed Properties
This does not store the value.They provide getter and optional setter to retrieve and set other properties value indirectly. Computed property also var not let becoz , their value is not fixed( and dependent on other param)
Ex:
struct Rect {
var origin = Point()
var size = Size()
var center: Point { // computed property
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
set(newCenter) {
origin.x = newCenter.x - (size.width / 2)
origin.y = newCenter.y - (size.height / 2)
}
}
}
Shorthand Setter Declaration
if setter has not provided name to new value then default it is taken as newValue.
struct AlternativeRect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
set {
origin.x = newValue.x - (size.width / 2)
origin.y = newValue.y - (size.height / 2)
}
}
}
Read-Only Computed Properties ,computed property with getter only. removing get keyword and its braces.
ex:
struct Cuboid {
var width = 0.0, height = 0.0, depth = 0.0
var volume: Double {
return width * height * depth
}
}
####Property Observers (like KVO)
You can add it to any stored property except for lazy stored property. (For computed property anyways require to call set method so no KVO needed ). You can add property observers to any inherited property (whether stored or computed-here becoz needed for parent class computed property if its source not available) by overriding the property within a subclass. You don’t need to define property observers for nonoverridden computed properties, because you can observe and respond to changes to their value in the computed property’s setter. Computed property setter is get called so no need of observer.
observer are:
-willSet is called just before the value is stored. New property value should be const parameter. If u have not provided name for it then it will take default as newValue.
-didSet is called immediately after the new value is stored. same as above const parameter. here default parameter name is oldValue when u did not provided.If you assign a value to a property within its own didSet observer, the new value that you assign replaces the one that was just set.
Ex:
class StepCounter {
var totalSteps: Int = 0 {
willSet(newTotalSteps) {
print("About to set totalSteps to \(newTotalSteps)")
}
didSet {
if totalSteps > oldValue {
print("Added \(totalSteps - oldValue) steps")
}
}
}
}
The willSet and didSet observers of superclass properties are called when a property is set in a subclass initializer, after the superclass initializer has been called. They are not called while a class is setting its own properties, before the superclass initializer has been called.
If you pass a property (that has observers to a function) as an in-out parameter, the willSet and didSet observers are always called. This is because of the copy-in copy-out memory model for in-out parameters: The value is always written back to the property at the end of the function.
We can add observer to global stored properties also.(Global variable). Global constants and variables are always computed lazily . do not need to be marked them with the lazy modifier.
Type Properties (use static keyword)
like static data member to class in c++. Can be var or let. computed static properties should be variable.
Unlike stored instance properties, you must always give stored type properties a default value. This is because the type itself does not have an initializer that can assign a value to a stored type property at initialization time.
Stored type properties are lazily initialized on their first access. They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they do not need to be marked with the lazy modifier.
If u want to override the computed type properties use class keyword.
class SomeClass {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 27
}
class var overrideableComputedTypeProperty: Int {
return 107
}
}
0 comments:
Post a Comment