什么使属性成为 Swift 中的计算属性 [英] What makes a property a computed property in Swift

查看:65
本文介绍了什么使属性成为 Swift 中的计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们从代码片段开始:

St Foo {var proA: Int = 0 {//需要初始化将设置{print("即将从\(proA)设置proA到\(newValue)")}已设置{print("已经将 proA 从 \(oldValue) 设置为 \(proA)")}}var ProB: Int {//不需要初始化返回 1}}让 foo = Foo()foo.proA = 23打印(foo.ProB)

以下是我个人对存储和计算属性的一些理解:

a:只有观察者的属性(willSet 和 didSet)不是计算属性而是存储属性(例如上面代码中的 proA 属性).

b:计算属性不能有初始化(见上面代码的注释).

c: setter 相当于属性观察者,属性观察者只是 setter + 变化前后的观察者.

问题:

1. 我想知道是什么使属性成为计算属性?只要该属性有一个 getter 并返回它就是一个计算属性,这是否正确?

2. 我的所有理解(a、b 和 c)都正确吗?如果没有,请您指出.

3. 为什么不允许初始化计算属性?(请参见下图)当我这样做时,编译器发出警告 Cannot call value of non-function type "int" 这个错误是什么意思?

非常感谢.

解决方案

首先,这是关于变量,而不是属性.任何变量都可以是计算变量.属性只是使用变量的一种方式.

我认为总的来说,您将带有 setter 观察者的存储变量与计算变量并排放置是一个大错误.他们毫无关系!

将计算变量视为在使用时看起来和行为类似于变量的东西——你得到和(也许)设置它——但实际上是一个函数(或一对函数). 这只是调用函数的一种紧凑方式.这就是全部.

另一方面,带有观察者的存储变量只是一个也有一些观察者的存储变量.

<小时>

好的,请回答您的问题:

<块引用>

  1. 我想知道是什么使属性成为计算属性?是否正确,只要该属性有一个 getter 并返回它就是一个计算属性?

是的.这是一个计算变量,因为您使用使其成为计算变量的语法(使用花括号)声明了它.

<块引用>

  1. 我的所有理解(a、b 和 c)都正确吗?如果没有,你会很高兴指出

是的.我认为你的c"很有见地:一个计算变量不需要一个 setter observer 因为它有(喘气!)一个 setter!

<块引用>

  1. 为什么不允许初始化计算属性?(请参见下图)当我这样做时,编译器发出警告无法调用非函数类型int"的值此错误的含义是什么?

计算变量有"一个值是没有意义的——它是计算出来的!这只是一些功能!— 所以给它分配一个初始"值是没有意义的.

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.

解决方案

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.

这篇关于什么使属性成为 Swift 中的计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆