0

I can update property value of class instance using dot notation However It doesn't work for structure.

Here is example:

struct Resolution {
    var width = 0

}

class VideoMode {
  var interlaced = false
 }


let someResolution = Resolution()
let someVideoMode = VideoMode()

For class:

someVideoMode.interlaced // false
someVideoMode.interlaced = true //true
someVideoMode.interlaced // now true 

For Struct :

 someResolution.width // 0
 someResolution.width  = 200 // throws an error says : someResolution is constant 

Question is :

someResolution and someVideoMode both are constants. I can change the property value of class instance without error not saying someVideoMode is constant.However I can not change the property value of struct.It throws an error says someResolution is constant

Why ?

Thank you !

shallowThought
  • 19,212
  • 9
  • 65
  • 112

1 Answers1

0

structs are Value types, while classes are References.

While you can set properties of a constant reference type, you can not change the reference itself.

let someVideoMode = VideoMode()
someVideoMode = VideoMode()

would cause an error.

You can find a detailed description in the documentation you actually got this sample from.

shallowThought
  • 19,212
  • 9
  • 65
  • 112