I'm trying to set the value of an instance variable dynamically at runtime in Objective-C.
Assume I have a class called stock which has an instance variable float price.
I have the following code:
float newPrice = 12.56;
Ivar variable = class_getInstanceVariable(NSClassFromString(@"stock"), "price");
float* pricePointer = (float*)((char *)c + ivar_getOffset(variable));
(*pricePointer) = newPrice;
NSLog(@"%f", [s price]);
Found the problem. Since the class stock contains the instance variable price as well as the property price, the value of the instance variable was not being set. Rather the value of the property. This is not going to work as property is a method.