1

我试图从 setter 选择器中找出原始属性名称。例如,我知道 setter 被调用setFoo:并且想要获取foo. 这应该是一个非常简单的字符串处理任务(删除set第一个字母并将其更改为小写),但我想知道在 Objective-C 运行时的某个地方是否有任何开箱即用的解决方案。

我想这样使用它:

@interface MyClass : NSObject

@property (nonatomic, assign) BOOL foo;

@end

@implementation MyClass

@dynamic foo;

+(BOOL)resolveInstanceMethod:(SEL)sel
{
    const char* selectorName = sel_getName(sel);
    objc_property_t getterProperty = class_getProperty([self class], selectorName);
    objc_property_t setterProperty = class_getProperty([self class], getPropertyNameFromSetterName(selectorName));
    if (getterProperty) {
        // now I know that the property was declared and I should provide
        // the getter implementation
    } else if (setterProperty) {
        // I should provide the setter implementation
    }
}

@end
4

0 回答 0