Swift - didSet{...} Property Observer

Swift - didSet{...} Property Observer

if you're not familiar with the 'didSet' property observer - this is for you:

with this simple trick you can execute a code when a property is changed.

e.g. - if we want a counter to count every time a property changed (in this example - when the button is pressed):

No alt text provided for this image

This is how it works: just add { didSet { //your code }} like this:

struct Entity{
??? private(set) var changedCounter = 0
??? var string: String? {
??????? didSet{
??????????? changedCounter += 1
??????? }
??? }
?? ?
??? init(str: String){
??????? self.string = str
??? }
}        


and here's the code for the ViewController to test it:

we create an entity from above, and each button press will change the string, and consequently the counter updates.

No alt text provided for this image

screenshot for the entity :

No alt text provided for this image
Netanel Stern

CEO and security engineer

4 个月

???? ??? ?? ?? ??????! ??? ????? ???? ?????? ???: ?????? ????? ??? ??????? ?????? ??????, ?????? ?????? ??????,?????? ????? ????????. https://chat.whatsapp.com/IyTWnwphyc8AZAcawRTUhR

回复

要查看或添加评论,请登录

Aviram Netanel的更多文章

  • UIKit vs. SwiftUI - TableView \ List

    UIKit vs. SwiftUI - TableView \ List

    I wrote 2 projects that shows a tableview \ list with expandable cells. The idea is simple: you have a table \ list…

    4 条评论
  • Swift - Building a custom framework

    Swift - Building a custom framework

    A quick tutorial for building and importing a custom SDK to your project. save it for a rainy day.

    1 条评论
  • Swift - the 'required' keyword

    Swift - the 'required' keyword

    Did you ever get this error? 'required' initializer 'init(coder:)' must be provided by subclass of..

    1 条评论
  • Swift - Array map, flatMap & compatMap

    Swift - Array map, flatMap & compatMap

    map, flatMap & compactMap are Array methods for manipulation. here are few simple examples to help you understand, and…

  • SwiftUI Pagination

    SwiftUI Pagination

    Pagination in SwiftUI & MVVM. In this example I'll show how to handle pagination: 1) Let's start with our ContentView:…

    2 条评论
  • The keyword 'some' - Swift \ SwiftUI

    The keyword 'some' - Swift \ SwiftUI

    If you ever started a SwiftUI project, you've probably noticed this 'some' keyword. so what's that? The keyword 'some'…

    7 条评论
  • Static - when do we use it?

    Static - when do we use it?

    First, if you don’t know the difference between Type & Instance: Type is the definition of a class \ struct . Instance…

    9 条评论
  • UIViewController - Lifecycle

    UIViewController - Lifecycle

    This is one of the most common questions in interviews, so you better know this one. In General Every view controller…

    9 条评论
  • @escaping closures in Swift

    @escaping closures in Swift

    In short: We will use @escaping when the closure needs to outlive the life of its function More: when we pass a closure…

    5 条评论
  • Arguments and Parameters

    Arguments and Parameters

    Arguments and Parameters What's the difference? Parameter is the variable in the declaration of the function. Argument…

    4 条评论

社区洞察

其他会员也浏览了