dimanche 20 février 2022

Has Swift in Version 5.5+ a Native Language/Framework Implementation of Property Observers?

I am currently catching up with the latest language development of Swift, porting old Objective-C code to Swift 5.5/5.6. Searching for property observer patterns I found a confusing amount of old methods, which seems to point back to previous Swift versions.

Yet, I found no native implementation of an observer pattern for object properties (like the native e.g. implementation of Codable).

So, before I start manually adding a custom observer pattern for my objects. My Question:

Is there a native implementation for property observers in Swift 5.5/5.6?

  • Without @objc
  • Without NSObject
  • No NotificationCenter
  • No delegate pattern, I need a pattern that allows multiple observers.

Fictive example to illustrate what I mean with property observer pattern:

class MyObject : Observable {

  var value = 0

}

class MyUser : Observer {
  func init(obj: MyObject) {
    obj.addObserver(self)
  }
  func deinit() {
    obj.removeObserver(self)
  }
  func onValueChanged(obj: AnyObject, name: String) {
    // ...
  }
}

Aucun commentaire:

Enregistrer un commentaire