Quantcast
Channel: What is the purpose of willSet and didSet in Swift? - Stack Overflow
Viewing all articles
Browse latest Browse all 12

Answer by knshn for What is the purpose of willSet and didSet in Swift?

$
0
0

You can also use the didSet to set the variable to a different value. This does not cause the observer to be called again as stated in Properties guide. For example, it is useful when you want to limit the value as below:

let minValue = 1var value = 1 {    didSet {        if value < minValue {            value = minValue        }    }}value = -10 // value is minValue now.

Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>