What makes a property a computed property in Swift

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?

enter image description here

Thank you very much.

+3
source share
4 answers

First, it's about variables, not about properties. Any variable can be a computed variable. A property is just one way to use a variable.

I think that in general you are making a big mistake when placing a stored variable with setter observers side by side with the calculated variable. They are not connected!

Think of the computed variable as what looks and acts like a variable, when you use it, you get and (possibly) set it, but it's actually a function (or a couple of functions). This is just a compact way to call a function. That is all there is.

A stored variable with observers, on the other hand, is only a stored variable, which also has some observers.


Ok, for your questions:

  • I wonder what makes a property a computed property? Is it correct that as long as the property has getter and return, this is a computed property?

Yes. This is a computed variable because you declared it using syntax that makes it a computed variable (with curly braces).

  1. Is all my understanding (a, b, and c) correct? If you weren’t good to point out

Yes. I think your "c" is quite perceptive: the computed variable does not need a setter observer, because it has (exhale)! Setter!

  1. 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 is not possible to call the value of a non-function of type "int". What is the value of this error?

It makes no sense when the calculated variable matters - it is calculated! these are just some features! - therefore, it makes no sense to assign it an "initial" value.

+3
source

A stored property is a property whose value is stored with an instance of a class or structure. The value can be changed, but the property can also be a constant. Thus, a stored property can be as simple as:

 var proA: Int let proB: Int var proC: Int = 0 

The calculated properties do not store the value. Therefore, you cannot assign a value to the computed property. The computed property must have a getter that returns a value. I widely use the term "computed property" as a property that returns the value of a function.

Computable Property Example

 var proA: Int { return proB * proC } 

Regarding your questions:

  • A computed property is a property that does not store a value and contains get to return the "computed" value of the property.
  • a correctly, b computed properties should not have initialization, c, if you mean willSet and didSet. Yes, they are like observers when the value of a property changes and changes accordingly.
  • Since the value of the computed property is not stored and will never be used, the compiler forbids it.

Hope this helps a bit.

+2
source
  • I wonder what makes a property a computed property? Is it correct that as long as the property has getter and return, this is a computed property?

If you define get { } inside a property declaration, it assigns that property to the computed property. And it cannot have an initial value, since when accessing a property, it will always call the get{} function declared in the property.

  1. As far as I understand (a, b and c) correctly? If you weren’t good to point out

    • a correct
    • b is not true.

    You cannot set the initial value for the computed property. Because, as I explained in question 1, it will always return the result get{} when you need access to this property.

    • c: 50% right

    setter, it can also be used to store newValue in another private variable, and you can do additional observing logic. Thus, to observe changes in the values ​​of the stored property, you use willSet and didSet You can define the observing logic for the computed property (which has getter and setter ) in the set{} declaration. But the main purpose of set {} is to save the value of another variable or, for example, UserDefaults .

  2. Why is it not allowed to initialize a computed property? (See the figure below) And when I do this, the compiler generates a warning. It is impossible to call the value of a non-function of type "int". What is the value of this error?

    The same answer

    Your code causes the compiler to get confused. When you set the initial value of a property in a declaration, the compiler tries to understand it as a stored property. But you also defined get{} for this property, and that means that it is a computed property and should always return 22 when accessing the property. Therefore, you must remove one of the two.

+1
source

a. Yes, a property with an observer only is a stored property, not a computed property. Observer properties. Tracks the value of a property whose value is initialized earlier and now it changes to what the stored property is. It is not applicable to the computed property, since it has no predefined value.

b. a computed property is a property whose value depends on other variables, we should only declare those properties that the computed property should be calculated using the values ​​of other variables, so its value cannot be initialized in advance. eg. - If we have 2 variables a and b. we need their added value, so we use a variable called "sum", then the sum will be declared as a computed property, and its get {} block will return (a + b) the sum of a and b and the value of the sum variable. Then in this case we cannot initialize the 'sum' property in advance, because it will be calculated using a and b.

with. The setter is not an observer; it sets the value of another variable or performs some actions related to other variables, while the property observer tracks changes in the value of its own associated variable. eg. it makes no sense to use a property observer for the sum variable, as described in b.

+1
source

Source: https://habr.com/ru/post/1270403/


All Articles