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

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.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...