本文介绍了我的isa混乱打破了KVO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施isa swizzling,因为我需要在某些对象的dealloc方法中进行一些操作.我覆盖-(Class)class;返回原始类的方法(如KVO一样).一切正常,直到我尝试将观察者添加到陷入困境的对象中为止.它只是崩溃了.

I'm trying to implement isa swizzling because I need some actions to happen in dealloc method of certain object. I'm overriding - (Class)class; method to return original class (as KVO does). Everything works fine, until I try to add observer to swizzled object. It just crashes.

  1. 0x00000000()中的0x00000000
  2. 0x0091d22a在_NSKeyValueRetainedObservationInfoForObject()
  3. -[NSObject(NSKeyValueObserverRegistration)_addObserver:forProperty:options:context:]()中的0x0092ec88
  4. -[NSObject(NSKeyValueObserverRegistration)addObserver:forKeyPath:options:context:]()
  5. 中的0x0092d6fd
  1. 0x00000000 in 0x00000000 ()
  2. 0x0091d22a in_NSKeyValueRetainedObservationInfoForObject ()
  3. 0x0092ec88 in -[NSObject(NSKeyValueObserverRegistration) _addObserver:forProperty:options:context:] ()
  4. 0x0092d6fd in -[NSObject(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:] ()

这是实现混乱的方法

- (void)swizzleClass
{
    NSString *proxyClassName = [NSString stringWithFormat:@"MDSwizzled_%@", NSStringFromClass(self->isa)];
    Class proxyClass = NSClassFromString(proxyClassName);

    if (!proxyClass)
        proxyClass = [self createProxyClassWithName:proxyClassName];

    object_setClass(self, proxyClass);
}

- (Class)createProxyClassWithName:(NSString *)proxyClassName
{
    const char *c_proxyClassName = [proxyClassName cStringUsingEncoding:NSUTF8StringEncoding];
    Class proxyClass = objc_allocateClassPair(self->isa, c_proxyClassName, 0);

    Class dummyClass = [MDDummy class];
    class_addMethodFromClass(proxyClass, dummyClass, @selector(dealloc));
    class_addMethodFromClass(proxyClass, dummyClass, @selector(class));

    objc_registerClassPair(proxyClass);
    return proxyClass;
}

MDDummy只是一种方便的类保存方法(此方法与添加原始函数没有区别.)

MDDummy it's just a class holding method in convinietn way (there is no difference between this and adding raw functions).

@implementation MDDummy
- (void)dealloc
{

    //Special thinngs

    [super dealloc];
}

- (Class)class
{
    return //original class;
}
@end

这是class_addMethodFromClass函数的实现:

Here's implementation of class_addMethodFromClass function:

void class_addMethodFromClass(Class class, Class sourceClass, SEL selector)
{
    Method method = class_getInstanceMethod(sourceClass, selector);
    IMP methodImplementation = method_getImplementation(method);
    const char *types = method_getTypeEncoding(method);
    class_addMethod(class, selector, methodImplementation, types);
}

推荐答案

您应该查看Mike Ash如何处理此问题: https://github.com/mikeash/MAZeroingWeakRef

You should check out how Mike Ash handles this: https://github.com/mikeash/MAZeroingWeakRef

摘要:处理KVO混乱的子类的方式有所不同-您必须在KVO子类中修补KVO方法...

Summary: handle swizzling a KVO-swizzled subclass differently--you'll have to patch the KVO methods in the KVO subclass...

这篇关于我的isa混乱打破了KVO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:04