覆盖子类的所有设置方法和获取方法 [英] Override all setters and getters of a subclass

查看:56
本文介绍了覆盖子类的所有设置方法和获取方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重写setter和getter并找到objc_property_t的类,而不必对每个属性分别进行设置.

I want to override the setter and getter and find the class of an objc_property_t without doing it individually for each property.

我得到了所有类似的属性:

I get all the properties like so:

unsigned int numberOfProperties;
    objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties);
    for (NSUInteger i = 0; i < numberOfProperties; i++) {
        objc_property_t property = propertyArray[i];
        NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];

        property.getter = SEL; //?
    }

这是我要如何重写getter和setter的示例-如果有更好的方法,请告诉我.也许是NSInvocation?

This is an example of how I want to override the getter and setter - if there is a better way, let me know. NSInvocation maybe?

- (UIImage *)backgroundImage
{
    return [self overrideGetterWithSelector:NSStringFromSelector(_cmd)];
}

- (void)setBackgroundImage:(UIImage *)backgroundImage
{
    [self overrideSetterForObject:backgroundImage forSelector:NSStringFromSelector(_cmd)];
}

还是有一种方法可以拦截发送给某个班级的所有消息?

Or is there a way to intercept all messages sent to a class?

我的目标是提出一种通用的方式来在两次启动之间存储类的属性.您可能想问为什么我不使用NSUserDefaultsNSKeyedArchiver.好吧,我正在使用NSKeyedArchiver-我不想手动覆盖每个单个的setter和getter.

My goal is to make a generic way to store properties of a class between launches. You probably want to ask why I don't use NSUserDefaults or NSKeyedArchiver. Well, I am using NSKeyedArchiver - I don't want to manually override every single setter and getter.

推荐答案

您只需在objc运行时中使用class_replaceMethod即可替换getter的实现.

You can just use class_replaceMethod from the objc runtime to replace the implementation of the getter.

示例:

- (void)replaceGetters {
    unsigned int numberOfProperties;
    objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties);
    for (NSUInteger i = 0; i < numberOfProperties; i++) {
        objc_property_t property = propertyArray[i];
        const char *attrs = property_getAttributes(property);
        NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];

        // property.getter = SEL; //?
        // becomes
        class_replaceMethod([self class], NSSelectorFromString(name), (IMP)myNewGetter, attrs);
    }
}

id myNewGetter(id self, SEL _cmd) {
    // do whatever you want with the variables....

    // you can work out the name of the variable using - NSStringFromSelector(_cmd)
    // or by looking at the attributes of the property with property_getAttributes(property);
    // There's a V_varName in the property attributes
    // and get it's value using - class_getInstanceVariable ()
    //     Ivar ivar = class_getInstanceVariable([SomeClass class], "_myVarName");
    //     return object_getIvar(self, ivar);
}

这篇关于覆盖子类的所有设置方法和获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆