It will take about 3 minutes to finish reading this article.
Example Code
1 | //arr1 |
arr1 declares a Stored-Property assigned value through a closure operation.
arr2 declares a read-only Computed-Property.
arr3 declares a read-only Computed-Property too.
arr4 declares a read-write Computed-Property too.
It can be seen that the Stored-Property can be directly read and written. The Computed-Property cannot be operated directly. It is only used for calculation and has no specific value.
Note:
- Computed-Properties can be used for classes, structures, and enumerations, while Stored-Properties can only be used for classes and structures.
- The Stored-Properties can be defined with the keyword ‘var’ or the keyword let’. The Computed-Property can only be defined with the keyword ‘var’.
- Computed-Properties do not directly store values, but provide a getter and an optional setter to indirectly obtain and set the values of other properties or variables.