Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes a property a computed property in Swift

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?

enter image description here

Thanks a lot.

like image 498
SLN Avatar asked Oct 11 '16 20:10

SLN


People also ask

What is computed property in Swift example?

Swift Computed Property For example, class Calculator { // define stored property var num1: Int = 0 ... } Here, num1 is a stored property, which stores some value for an instance of Calculator . Here, sum is a computed property that doesn't store a value, rather it computes the addition of two values.

What is the difference between computed property and lazy property?

Swift's lazy properties let us delay the creation of a property until it's actually used, which makes them like a computed property. However, unlike a computed property they store the result that gets calculated, so that subsequent accesses to the property don't redo the work.

How does a computed property work?

A computed property is used to declaratively describe a value that depends on other values. When you data-bind to a computed property inside the template, Vue knows when to update the DOM when any of the values depended upon by the computed property has changed.

What is the difference between a computed property and a property set to a closure in Swift?

In short, the first is a stored property that is initialized via a closure, with that closure being called only one time, when it is initialized. The second is a computed property whose get block is called every time you reference that property.


2 Answers

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.

like image 196
matt Avatar answered Sep 21 '22 05:09

matt


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.

like image 28
Carien van Zyl Avatar answered Sep 21 '22 05:09

Carien van Zyl