jeudi 2 novembre 2017

object_setIvar method will cover/reset field assigned

I used reflection of Objective-c to assign value to object. If fields didn't sort by name and the assigning-value operation sort by name. The value of field assigned will be reset/covered. I think the memory offset is messy when assigning operation, but I don't know how to correct it. Sample: the class definition:

@interface TestContract : NSObject
{
    int age;
    int size;
    NSString* name;

}
@end

if I assign name field first using below code

object_setIvar(targetObj, ivarType, [v copy]);

then assign size field using code

((void(*)(id, Ivar, int))object_setIvar)(targetObj, ivarType, v);

the name field will be reset to empty string(@"")





Aucun commentaire:

Enregistrer un commentaire