๐ธproperty observer (willSet/didSet)
โฑ๐ง under construction ->
Swift โฉ type โฉ property โฉ stored โฉ property observer
โ๏ธ willSet/didSet vs. get/set
ๅช้็ฃๆงๅฑฌๆง่ฎๅๆๅท่กๅฏไฝ็จ๏ผ็จ willSet/didSet (property observer)
้ไธปๅ่ชฟๆดๆ้ๅถๅฑฌๆงๅผๆ๏ผ็จ get/setใ
willSet๏ผๅจ่ฎๆดๅ็ๆบๅๅทฅไฝ๏ผๅฆ๏ผๆชขๆฅใๅไปฝ่ๅผๆ็ผๅบ่ญฆๅใ
didSet๏ผ็ฃๆง่ฎๆดๅพ็่็๏ผๅฆ๏ผ้ๅถ็ฏๅใ่งธ็ผไบไปถๆๆดๆฐ UIใ
willSet ็กๆณ้ปๆญขๆๆน่ฎๅฑฌๆงๅผ็่จญๅฎ๏ผไฝ didSet ๅฏ้ฒ่กไฟฎๆญฃใ
willSet/didSet ๆฏ็ตฆ stored property ็จ็๏ผcomputed property ไธ่ฝ็จโ
โ๏ธ stored vs. computed
Property observers are called every time a propertyโs value is set, even if the new value is the same as the propertyโs current value.
willSet
is called just before the value is stored.didSet
is called immediately after the new value is stored.
You can add property observers in the following places:
stored properties that you defineโฑinherit.
computed properties that you inherit.
Last updated
Was this helpful?