What makes a property a computed property in Swift

人盡茶涼 提交于 2019-12-10 10:23:38

问题


Let's started with the code snippet:

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 understandings about the the stored and computed property:

a: Property with only the observer (willSet and didSet) is not a computed property but a stored property (ex. the proA property in the code above).

b: Computed property must not has initialization (See the comments of the code above).

c: setter is kind of equal to the property observer, the property observer is just the setter + the observer to of the before and after mutating.

Questions:

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

2. Are all my understandings (a, b & c) correct? If not, would be nice of you to point out.

3. Why is it not allowed to initialize an computed property? (Please see the figure below) And when I do so the compiler gives out the warning Cannot call value of none-function type "int" What's the meaning of this error?

Thanks a lot.


回答1:


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

I think on the whole you are making a big mistake in putting a stored variable with setter observers side by side with a computed variable. They are unrelated!

Think of a computed variable as something that looks and acts like a variable when you use it — you get and (maybe) set it — but is in fact a function (or a pair of functions). It is just a compact way of calling a function. That's all it is.

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


Okay, on to your questions:

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

Yes. It's a computed variable because you declared it using the syntax that makes it a computed variable (with the curly braces).

  1. Are all my understandings (a, b & c) correct? If not would be nice of you to point out

Yes. I think your "c" is quite insightful: a computed variable does not need a setter observer because it has (gasp!) a setter!

  1. Why is it not allowed to initialize an computed property? (Please see the figure below) And when I do so the compiler gives out the warning Cannot call value of none-function type "int" What's the meaning of this error?

There is no sense in which a computed variable "has" a value — it is computed! it's just some functions! — so it makes no sense to assign it an "initial" value.




回答2:


A stored property is a property of which the property value is stored together with the instance of the class or struct. The value may 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

Computed properties do not store a value. Thus you cannot assign a value to a computed property. A Computed property should have a getter that returns a value. I a broad term, you can think of a computed property as a property that returns the value of a function.

Example of Computed Property

var proA: Int {
    return proB * proC
}

With regards to your questions:

  1. Computed Property is therefor a property that do not store a value, and contains a get to return the 'computed' value of the property.
  2. a is correct, b computed properties should not have initialization, c if you mean willSet and didSet. Yes they are like observers for when the property's value will change and did change respectively
  3. Since the value of a computed property is not stored and will never be used, the compiler forbids it.

Hope this helps a bit.




回答3:


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

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

  1. Are all my understandings (a, b & c) correct? If not would be nice of you to point out

    • a is correct
    • b is wrong.

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

    • c : 50% right

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

  2. Why is it not allowed to initialize an computed property? (Please see the figure below) And when I do so the compiler gives out the warning Cannot call value of none-function type "int" What's the meaning of this error?

    Same answer

    Your code makes compiler to be confused When you set initial value on the property on declaration, the compiler tries to understand it as stored property. But you also defined get{} for this property, and it means it is computed property and should always return 22 when you access the property. So you should remove one of two.




回答4:


a. Yes,a property with only observer is a stored property not a computed property.Beacuase property observer tracks the value of a property whose value has initialised previously & it's now changing ,that's a stored property. It's not applicable for a computed property since it has no predefined value

b. computed property is a property whose value depends on other variables, we should declare only those properties as computed property , who needs to be computed using value of another variables ,so it's value cannot be initialised in advance. for e.g. - If we have 2 variables a & b. we need their addition value , so a variable named 'sum' is used , then sum will be declared as a computed property & its get{} block will return (a+b) that's sum of a & b & the value of sum variable.Then in this case we can't initialise property 'sum' in advance because it will be computed using a & b.

c. Setter is not an observer it sets value of another variable or performs some actions related to other variables whereas a property observer tracks changes in value of its associated variable itself. for e.g. it's meaningless to use a property observer for variable 'sum' as described in point b .



来源:https://stackoverflow.com/questions/39986129/what-makes-a-property-a-computed-property-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!