ios - How to Apply KVO to @dynamic properties created on runtime? -


i'm doing best explain how works, it's pretty confusing , lengthy. let me know if there's can clarify.

i stucked in kvc , kvo concepts.
have entry class (nsobject).
entry class has private variable objectproperties (nsdictionary) who's value comes server.

suppose objectproperties has key "price", "discount", etc want create dynamic properties in entry class's instance.

these key may vary according response , dynamic variables.

now when user want use kvo on same properties not accessible.

for creating dynamic properties , it's getter/setter:

-(nsstring*)propname:(nsstring*)name {     name = [name stringbyreplacingoccurrencesofstring:@":" withstring:@""];     nsrange r;     r.length = name.length -1 ;     r.location = 1;     nsstring* firstchar = [name stringbyreplacingcharactersinrange:r withstring:@""];     if([firstchar isequaltostring:[firstchar lowercasestring]])     {return name;}     r.length = 1;     r.location = 0;     nsstring* therest = [name stringbyreplacingcharactersinrange:r withstring:@""];     return [nsstring stringwithformat:@"%@%@", [firstchar lowercasestring] , therest]; }  -(nsstring*)settername:(nsstring*)name {     name = [self propname:name];     nsrange r;     r.length = name.length -1 ;     r.location = 1;     nsstring* firstchar = [name stringbyreplacingcharactersinrange:r withstring:@""];     r.length = 1;     r.location = 0;     nsstring* therest = [name stringbyreplacingcharactersinrange:r withstring:@""];     return [nsstring stringwithformat:@"set%@%@", [firstchar uppercasestring] , therest]; }   -(nsstring*)propnamefromsettername:(nsstring*)name {     nsrange r;     r.length = 3 ;     r.location = 0;     nsstring* propname = [name stringbyreplacingcharactersinrange:r withstring:@""];     return [self propname:propname];  }   -(nsstring*)ivarname:(nsstring*)name  {     nsrange r;     r.length = name.length -1 ;     r.location = 1;     nsstring* firstchar = [name stringbyreplacingcharactersinrange:r withstring:@""].lowercasestring;   if([firstchar isequaltostring:@"_"])         return name;     r.length = 1;     r.location = 0;     nsstring* therest = [name stringbyreplacingcharactersinrange:r withstring:@""];     return [nsstring stringwithformat:@"_%@%@",firstchar, therest];  }   nsobject *getter(id self, sel _cmd) {     nsstring* name = nsstringfromselector(_cmd);     nsstring* ivarname = [self ivarname:name];     ivar ivar = class_getinstancevariable([self class], [ivarname utf8string]);     return object_getivar(self, ivar); }  void setter(id self, sel _cmd, nsobject *newobj) {     nsstring* name = [self propnamefromsettername:nsstringfromselector(_cmd)];     nsstring* ivarname = [self ivarname:name];     ivar ivar = class_getinstancevariable([self class], [ivarname utf8string]);     id oldobj = object_getivar(self, ivar);     if (![oldobj isequal: newobj])     {         object_setivar(self, ivar, newobj);         [newobj copy];     } }  -(nsdictionary *)createproperties:(nsarray *)propnames {     nsmutabledictionary* keys = [[nsmutabledictionary alloc]init];     for(nsstring* key in propnames)     {         nsstring* propname = [self propname: key];         nsstring* ivarname = [self ivarname:propname];          class_addivar([self class], [ivarname utf8string] , sizeof(nsobject*), log2(sizeof(nsobject*)), @encode(nsobject));          objc_property_attribute_t a1 = { "t", "@\"nsobject\"" };         objc_property_attribute_t a2 = { "&", "" };         objc_property_attribute_t a3 = { "n", "" };         objc_property_attribute_t a4 = { "v", [ivarname utf8string] };         objc_property_attribute_t attrs[] = { a1, a2, a3, a4};          class_addproperty([self class], [propname utf8string], attrs, 4);         class_addmethod([self class], nsselectorfromstring(propname), (imp)getter, "@@:");         class_addmethod([self class], nsselectorfromstring([self settername:propname]), (imp)setter, "v@:@");          id val = [self.objectproperties objectforkey:key];         [keys setvalue:val forkey:propname];     }     return keys; } 

but when user want observe properties, not accessible in user class.

i confused how create dynamic properties , applying kvo.

i have tried making subclass of entry class ie (myentry user creates @ end) , defining variables there. how set value there? want property readonly.

first of all: cannot create ivars after instantiating class. is, because memory footprint of created instances change. surprising -createproperties: instance method.

additionally kvo works subclassing class of observed instance @ runtime , assigning new class observed instance (isa-swizzling). problem, if change base class after registering kvo.

however, said applies automatic kvo. can use manual kvo instead. add kvo notifications accessors.

but commented: should rethink whole concept.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -