swift结构体常量的存储属性

    xiaoxiao2021-03-25  136

    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: 如果你创建一个结构体实例并且把实例赋值给一个常量,你不能够修改这个常量的属性,即使这些属性是变量。 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. 相同的情况对于类却不同,因为类实例是引用类型。如果你把类实例赋值给一个常量的话,你依然可以改变他的变量属性的值。

    转载请注明原文地址: https://ju.6miu.com/read-11785.html

    最新回复(0)