Start with a snippet of code:
St Foo { var proA: Int = 0 { // needs initialization willSet { print("about to set proA to \(newValue) from \(proA)") } didSet { print("already set proA to \(proA) from \(oldValue)") } } var ProB: Int { // do not needs initialization return 1 } } let foo = Foo() foo.proA = 23 print(foo.ProB)
Here are some of my personal considerations regarding a stored and computed property:
a: A property with an observer only (willSet and didSet) is not a computed property, but a stored property (for example, the proA property in the code above).
b: The computed property must not have initialization (see comments on the code above).
c: setter is equal to the property observer, property observer is only setter + observer before and after mutation.
Questions:
1. I wonder what makes a property a computed property? Is it correct that as long as the property has getter and return, is this a computed property?
2. Is all my understanding (a, b, and c) correct? If not, it would be nice if you noted.
3. Why is it not allowed to initialize a computed property? (See the figure below) And when I do this, the compiler issues a warning. It cannot call the value of a non-function like "int". What is the value of this error?

Thank you very much.
source share