ios - Swift: property observers for computed properties -


as far know, swift allows set property observers either stored , computed properties. if computed property value depends on backing store, property observers not fired when these backing store values changed:

public class baseclass {     private var privatevar1: int = 0     private var privatevar2: int = 0     public var property: int {         {             return privatevar1 * privatevar2         }         set {             print("some setter without effect")         }     }     private func changesomevalues() {         privatevar1 = 1         privatevar2 = 2     }  }  public class subclass : baseclass {     override var property: int {         didset {             print("didset \(property)")         }     } } 

didset of subclass isn't called when changesomevalues called.

let's consider case: have such baseclass in third-party framework. define subclass in our app. question is: how can rely on subclass observers without knowledge property nature: stored (and can rely on observers) or computed (and can't expect firing observers each time when expect it)? possible? if no, incapsulation violation?

that behaviour normal. there no way compiler know backing store corresponds computed property. backing store in case made of private variables not accessible outside class itself. place "under hood" change can occur in base class. class's prerogative use calculated properties (which trigger observers) or backstore (which not).

in example, assuming never want allow "invisible" changes, changesomevalues() function breaking own rules , not respecting contract promised subclasses , callers.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -