Set a default value for a property with defined getter and setter

properties, swift

Solution

In Swift, getters and setters are used for computed properties - there is no storage for the property and thus, in your case, `simpleDescription` can't be set in a setter.

If you need a default value, use:

class SimpleClass {
  var simpleDescription: String = "default description"
}

if you want to initialize use:

class SimpleClass {
  var simpleDescription: String
  init (desc: String) {
    simpleDescription = desc
  }
}

Problem

I have a very simple class ``` class SimpleClass { var simpleDescription: String { get { return self.simpleDescription } set { self.simpleDescription = newValue } } } ``` What is the correct way to define a default value for the `simpleDescription` variable?

Original source

Related problems