1

セッターセレクターから元のプロパティ名を把握しようとしています。たとえば、セッターが呼び出され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