[SWIFT] behavior of didSet and willSet

Here's what I created to understand didSet.

--Can be recalled when the value of a property changes. --didSet can be reset according to the changed value.

class Test {
    var num: Int = 0 {
        willSet {
            print("The value of num is\(newValue)Is likely to be changed to")
        }
        didSet {
            print("The value of num is\(self.num)Changed to")
            if self.num > 100 {
                print("Since num was 100 or more, return it to 20")
                self.num = 20
            }
        }
    }
}

var test = Test()
test.num = 102
print(test.num)//20

Recommended Posts

behavior of didSet and willSet
[Swift] Property observers willSet and didSet
Behavior of ThreadPoolTaskExecutor
Summary of ToString behavior with Java and Groovy annotations
Behavior is different between new and clear () of ArrayList
Overview of Docker and containers
Setup of JMeter and jEnv
I didn't understand the behavior of Java Scanner and .nextLine ().
Background and mechanism of Fabric-loader
Summary of FileInputStream and BufferedInputStream
Combination of search and each_with_index
Judgment of JSONArray and JSONObject
Java beginners briefly summarized the behavior of Array and ArrayList
Operator of remainder and exponentiation (exponentiation)
Advantages and disadvantages of Java
Detailed behavior of Stream API Stateful intermediate operation and short-circuit termination operation
About the behavior of ruby Hash # ==
Basics of conditional branching and return
About fastqc of Biocontainers and Java
Proper use of redirect_to and render
This and that of the JDK
[Swift] Advantages and disadvantages of Storyboard
Proper use of Mockito and PowerMock
[Java] Judgment of identity and equivalence
[Rails] Differences and usage of each_with_index and each.with_index
About removeAll and retainAll of ArrayList
This and that of Core Graphics
Default implementation of Object.equals () and Object.hashCode ()
Application of downcase and slice methods
This and that of exclusive control
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many