Swift中的属性和变量之间有什么区别? [英] What is the difference between a property and a variable in Swift?

查看:171
本文介绍了Swift中的属性和变量之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一些初始教程中,我看到属性属于一个类,并且本质上是C ++世界中使用的全局变量"(在几年前进行了编码).我还将变量看作更多的本地"实体,仅在方法中使用/将信息存储在方法中.

From a few initial tutorials, I see that properties belong to a Class and are essentially 'global variables' as used in the C++ world (coded in this years ago). I also see variables as more of a 'local' entities only used / storing information within a method.

然后,我遇到了这个Quora线程: https://www.quora.com/Apple-Swift-programming-language/What-is-the-difference-between a属性和一个变量 现在,我看到属性能够执行与其调用关联的代码.这很酷,但同时也给我带来了很多其他问题.

Then I came across this Quora thread: https://www.quora.com/Apple-Swift-programming-language/What-is-the-difference-between-a-property-and-a-variable Now I see properties being able to execute code associated with their invocation. This is very cool, but also opened up a whole bunch of other questions for me.

还有其他简单明了的方式来记住属性和变量之间的区别吗?

Are there other simple and clear ways to remember the distinction between a property and a variable?

推荐答案

局部变量就是您要使用的东西.您可以完全控制这些内容,并且如果您在函数中更改变量,则函数外的任何事物都将一无所知.如果我编写了一个框架并使用了它,并且决定更改某个函数的局部变量,那么使用我的框架的应用程序将继续正常工作,就像什么都没有改变一样.

Local variables are just things that you work with. You have full control over these, and if you change a variable in a function, nothing outside of your function is ever gonna know. If I write a framework and you use it, and I decide to change something about a function's local variables, your app that uses my framework will keep working just as if nothing changed.

另一方面,类描述了合同.使用课程时,您可以访问他们公开宣传的所有内容.这意味着,如果我编写了一个框架并使用了该框架,并且曾经更改或删除了某个类的公共成员,则如果您以前使用该成员,则代码将被破坏.

Classes, on the other hand, describe a contract. When you use a class, you have access to everything they publicly advertise. This means that if I write a framework and you use it, if I ever change or remove a public member on a class, your code will break if you were previously using that member.

因此,在许多语言中,将实例变量标记为公共变量是一种不好的做法.没有逻辑的实例变量,如果我想在某个时候更改某个字段时触发某件事,或者如果我想完全删除该字段(而是报告子对象或某物中的值),那么我就陷入困境更改公共合同(例如,使用一对get/set方法转换该字段),并可能破坏您的代码.

For this reason, in many languages, it's bad practice to mark instance variables as public. Instance variables having no logic attached, if I want at some point to trigger something when a field is changed or if I want to remove the field entirely (and instead report a value in a sub-object or something), then I'm stuck with changing the public contract (turning the field in a pair of get/set methods, for instance), and possibly breaking your code.

由于这个原因,Swift使属性成为间接的. Swift属性在大多数情况下都可以视为愚蠢的值,但是如果您需要将存储值更改为计算值之类的东西,则可以在不更改类接口的情况下进行操作.这样,您就不会破坏依赖于该属性的现有代码.

Swift makes properties an indirection for this reason. Swift properties can be treated as dumb values for the most part, but if you ever need to change from a stored value to a computed value or something, you can do it without changing your class's interface. That way, you don't break existing code that relies on the property.

这篇关于Swift中的属性和变量之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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