加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:27:32 所属栏目:百科 来源:网络整理
导读:在 Swift中,我们用let表示一个不可变的变量. 我不明白的是你改变他们的属性的原因.例如: let lbl = UILabel()lbl.textAlignment = .Right() 为什么要更改textAlignment?通过改变属性,我们还没有改变应该是常量的变量lbl吗? 根据 Swift Programming Langua
在 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读