objective c - Is there a way to dynamically allocate or assign values to ivars of a class at runtime in Cocoa / Cocoa Touch? -
i'm sure can dynamically determine ivars of class @ run time. there way allocate determined ivars during run time? tia..
the short answer , 1 want is: [obj setvalue:@"woof" forkey:@"dog"];
and ramblings...
you can using runtime find of ivars of class with:
ivar * class_copyivarlist(class cls, unsigned int *outcount)
eg:
id someobj; //assigned elsewhere unsigned int count = 0; ivar * ivarlist = class_copyivarlist([someobj class],&count); for(;count>0;count--) { ivar testvar = ivarlist[count]; //do test ivar } free(ivarlist);
i sure wouldn't ever want except perhaps in debugging scenario... introspecting private frameworks etc.
or if know name of ivar can use:
ivar object_setinstancevariable(id obj, const char *name, void *value)
or more objc way of [obj setvalue: @"woof" forkey:@"dog"]
fail through setting ivar.
what potentially do, , have had in past introspect properties list:
with analogous call to: objc_property_t * class_copypropertylist(class cls, unsigned int *outcount)
can useful copying unknown objects, , debugging.
Comments
Post a Comment