SLN SLN - 6 months ago 10
Swift Question

What's the connection between gettable, settable and stored property, computed property

I don't think I'm crystal clear on the connection between gettable, settable and stored property and computed property.

Can I say that the gettable is somehow related to stored property and settable is somehow related to computed property

Thanks so much for your time and help for a beginner like me

Answer

Computed properties:

  • Are not really properties at all
  • Do not have memory that stores their value
  • Are methods that present themselves as properties
  • Have a get and set method that can be overridden
  • Can be gettable, settable, or both.
  • Example usage:
    • Get a temperature in Celcius from a stored property that's stored in Kelvin
    • Get the area of a rectangle that's stored in a width and height stored property

Stored properties:

  • Are just like regular variables in other languages
  • Have memory that stores their value
  • Have a willSet(_:) and didSet(_:) method that can be overriden
  • Can always be read, but can be read only.
  • Example usage:
    • Store a temperature in Kelvin
    • Store a width and a height of a rectangle