Read Properties chapter of The Swift programming language
Read Properties chapter of The Swift programming language. Easy? Even here I have found something new:
struct Point {
var x = 0.0, y = 0.0
}
struct Size {
var width = 0.0, height = 0.0
}
struct Rect {
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(newCenter) { // <- dodn't know it's possible here
origin.x = newCenter.x - (size.width / 2)
origin.y = newCenter.y - (size.height / 2)
}
}
}
This is interesting note:
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. For a detailed discussion of the behavior of in-out parameters, see In-Out Parameters.
Section Property Wrappers is quite interesting as well. And this is new for me Projecting a Value From a Property Wrapper. Understood very little.
In section Global and Local Variables see clearly stated first time:
The capabilities described above for computing and observing properties are also available to global variables and local variables.
Subsection Type Property Syntax of section Type Properties is also bring something new:
You define type properties with the
static
keyword. For computed type properties for class types, you can use theclass
keyword instead to allow subclasses to override the superclass’s implementation.
Hm, from that could be good interview question.
Read Methods chapter of The Swift programming language.
Same finding with class
and static
keywords for type method like for type properties:
You indicate type methods by writing the
static
keyword before the method’sfunc
keyword. Classes can use theclass
keyword instead, to allow subclasses to override the superclass’s implementation of that method.
Neither of chapters explains how to call type property or method having instance. It’s expecially interesting to know how it works in case of override (classes only). Are all ovverides in chain get called? What’s called if overritten property (or function) is called when instance is casted to base class? Which versions get called?