为什么不可变对象的属性在Swift中是可变的?

为什么不可变对象的属性在Swift中是可变的?,第1张

概述在 Swift中,我们用let表示一个不可变的变量. 我不明白的是你改变他们的属性的原因.例如: let lbl = UILabel()lbl.textAlignment = .Right() 为什么要更改textAlignment?通过改变属性,我们还没有改变应该是常量的变量lbl吗? 根据 Swift Programming Language,常量结构的属性也是常量,但常量类可以具有可变属性 在 Swift中,我们用let表示一个不可变的变量.

我不明白的是你改变他们的属性的原因.例如:

let lbl = UILabel()lbl.textAlignment = .Right()

为什么要更改textAlignment?通过改变属性,我们还没有改变应该是常量的变量lbl吗?

根据 Swift Programming Language,常量结构的属性也是常量,但常量类可以具有可变属性.

用他们的话说,

If you create an instance of a structure and assign that instance to a constant,you cannot modify the instance’s propertIEs,even if they were declared as variable propertIEs…

The same is not true for classes,which are reference types. If you assign an instance of a reference type to a constant,you can still change that instance’s variable propertIEs.

总结

以上是内存溢出为你收集整理的为什么不可变对象的属性在Swift中是可变的?全部内容,希望文章能够帮你解决为什么不可变对象的属性在Swift中是可变的?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1033131.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存