为什么值类型的常量实例不能更改其属性,而引用类型的常量实例却不能更改其属性? [英] Why is constant instance of a value type can NOT change its properties while constant instance of a reference type can?

查看:188
本文介绍了为什么值类型的常量实例不能更改其属性,而引用类型的常量实例却不能更改其属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift的新手,正在尝试学习Property的概念。我从 swift编程语言2.1中看到了下面的语句和代码。

I'm new to Swift and is trying to learn the concept of Property. I saw the statements and code below from "swift programming language 2.1".

struct FixedLengthRange {
    var firstvalue: Int
    let length: Int
}

let rangeOfFourItems = FixedLengthRange(firstvalue: 0, length: 4)
rangeOfFourItems.firstvalue = 8 //error: cannot assign to property: rangeOfFourItems is a "let" constant

这本书为错误提供了以下解释:

And the book provided the following explanation for the error:


此行为是由于结构为值类型。将值类型的实例
标记为常量时,其所有属性也都标记为常量。

This behavior is due to structures being value types. When an instance of a value type is marked as a constant, so are all of its 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.

为什么值类型的常量实例不能更改其属性,而引用类型的常量实例却不能更改其属性?背后的原因是什么?这本书确实说了怎么做,但没有解释原因。我认为这是一种很好的做法,了解事物状态的背后原因。

Why is constant instance of a value type can NOT change its properties while constant instance of a reference type can? What is the reason behind it? The book did say how but failed to explain why. I think it is good practice to understand the reasons behind how things the way they are. Could someone please kindly explain it to me?

推荐答案


为什么值类型的常量实例可以不更改其属性

why is constant instance of a value type can NOT change its properties

因为值类型被视为不可分割的单位:它在赋值时被复制,并作为参数传递,就像一个副本,因此使用const-ness可以锁定整个 struct 。从某种意义上说, rangeOfFourItems 变量表示结构本身,而不是结构的指针或引用。

Because value type is treated as an indivisible unit: it gets copied on assignment, passing it as a parameter behaves like a copy, and so using const-ness locks down the entire struct. In a sense, rangeOfFourItems variable represents the structure itself, not a pointer or a reference to it.


而引用类型的常量实例可以吗?

while constant instance of a reference type can?

这并不是完全正确的说法,即声明const变量为引用类型也使实例保持不变。只有引用是常量,而不是实例。

This is not entirely correct to say that declaring a const variable of reference type makes the instance constant as well. Only the reference is constant, not the instance.

如果考虑一下,这是可以有意义地实现的唯一方法,因为多个变量可以通过-引用实例。如果这些变量之一是常量,而另一个不是常量,则将非常量引用分配给常量变量可能无法锁定所引用的对象,这也会锁定非常量引用:

If you think about it, that is the only way this could be meaningfully implemented, because multiple variables can reference the same by-reference instance. If one of these variables is constant and the other one is not constant, assigning a non-const reference to a constant variable could not possibly lock down the referenced object, which would lock out the non-const reference as well:

var a = ByRefType()
let b = a; // b is a constant reference to the same instance as "a"
a.property = newValue; // Prohibiting this assignment would be inconsistent

当然是常量变量本身(例如 a 不同。

Of course the constant variable itself (e.g. b above) could not be re-assigned, unlike the non-constant variable a.

这篇关于为什么值类型的常量实例不能更改其属性,而引用类型的常量实例却不能更改其属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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