如何从默认的setter名称获取属性名称?

时间:2013-02-25 09:26:19

标签: objective-c properties

我试图从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

0 个答案:

没有答案