You can have a look at the past article for the basic details about swift properties.

As we code in Xcode with Swift, we tend to use properties lots of time. But the property I usually used was ‘stored properties’. I used other properties only for the specific cases. I wanted to learn and use other properties more often. So in this article, we’ll have a look at other types of properties.


Computed Properties

Computed properties do not actually store something in the properties. But they support getter and setter to indirectly get or set values. Apple gives an example like this.


This example is an example of a square. It has origin, center, and size of the square. By computed property ‘center’ it gets and sets the center of the square. It can be used like this.


Some extra things that we should know about computed properties are that they can have shorthand setter declaration & read-only computed properties.

This can be shortened like this.

If this happen, a default name is ‘newValue’.

And read-only computed properties are the computed properties that only have getter. Only having getter is possible but, only having setter is impossible.


Property Observers

Property observers tend to observe changes and respond to them by changing their values. It has a willSet and didSet.

The Apple document gives a example like this.


This can be used like this.


Type Properties

Type properties are properties that allow you to define non-instance properties. This means you can use this property anytime after initializing it.

It can be initialized by using the key ‘static’.

For example, type properties can be used like this.


Related articles

Swift closures review

• Apple swift enumerations review



Apple Documents

댓글 남기기

Please enter your comment!
Please enter your name here