swift - struct and class didSet procedures behaving differently -
i've filed radar report, i'm not sure do.
what have, struct, , struct instantiated , aggregated class instance.
since struct object, has setters , getters, set/get observers.
i'm using these observers re-index array of struct instances.
the deal is, struct has "visible" flag (in implementation). if flag turned off (set false), container class needs re-index structs, remove now-invisible (or now-visible) instance index.
so, best way that, is, when instantiate struct instance, hand reference container instance, , set didset { mycontainer.reindex() } handler.
the problem structs , classes seem behave differently. suspect it's value/reference issue.
in class, external value of instance set time didset called. means can call external object re-index, , see new value.
in struct, not seem case until after didset executed.
i'll include playground submitted radar report.
my question is: think structs should have setter/getter observers? should have setter/getter @ all?
i have workaround. call indexer after changing value. it's not graceful.
the playground (in below code, see different values emitted during didset handlers):
struct structa {     let classbinstance:classb      init ( inclassb:classb )     {         self.classbinstance = inclassb         self.testproperty = 0     }      var testproperty:int = -1     {         didset         {             self.classbinstance.reacttosetstruct ( )         }     } }  class classa {     let classbinstance:classb      init ( inclassb:classb )     {         self.classbinstance = inclassb         self.testproperty = 0     }      var testproperty:int = -1     {         didset         {             self.classbinstance.reacttosetclass ( )         }     } }  class classb {     var structainstance:structa! = nil     var classainstance:classa! = nil      init()     {         self.structainstance = structa ( inclassb:self )         self.classainstance = classa ( inclassb:self )     }      func dosetstruct()     {         println ("before set: struct property value \(self.structainstance.testproperty)")         self.structainstance.testproperty = 1         println ("after set: struct property value \(self.structainstance.testproperty)")     }      func dosetclass()     {         println ("before set: class property value \(self.classainstance.testproperty)")         self.classainstance.testproperty = 1         println ("after set: class property value \(self.classainstance.testproperty)")     }      func reacttosetstruct()     {         println ("struct value \(self.structainstance.testproperty)")     }      func reacttosetclass()     {         println ("class value \(self.classainstance.testproperty)")     } }  let classtest:classb = classb()  classtest.dosetstruct() classtest.dosetclass()       
 
Comments
Post a Comment