I need to call the setter using the reflection. to do that i need selector. but the setterName var is nil in my code. I have setter and getter in my class. This is my code
objc_property_t *allProperties = class_copyPropertyList([object class], &allPropertyCount);
objc_property_t prop = class_getProperty(cls, propName);
for (unsigned int i = 0; i < allPropertyCount; i++) {
prop = allProperties[i];
}
char *setterName = property_copyAttributeValue(prop, "S");
SEL selector = NSSelectorFromString(setterName);
if([object respondsToSelector:@selector(selector)]){
return selector;
}
return nil;
The “S” and “G” attributes are only set if the property defines a custom setter or getter, respectively. Your code needs to check if
setterNameis NULL and, if so, generate the setter name from the property name.