首页  >  问答  >  正文

ios - hook dealloc方法装逼失败,咋弄了这下...(该如何收场)

2017-03-13 12:47:33.978 JJYSPlusPlus[9734:5104425] -[WYPlaceholderTextView removeObserver:]: unrecognized selector sent to instance 0x7ffb4e293e00
(lldb)
+ (void)load {
    // Swizzle dealoc to remove observer when current object is deallocated.
    Class classToSwizzle = [UITextView class];
    //    NSString *className = NSStringFromClass(classToSwizzle);
    SEL deallocSelector = sel_registerName("dealloc");

    __block void (*originalDealloc)(__unsafe_unretained id, SEL) = NULL;

    id newDealloc = ^(__unsafe_unretained id objSelf) {
        [objSelf removeObserver:self];

        if (originalDealloc == NULL) {
            struct objc_super superInfo = {
                .receiver = objSelf,
                .super_class = class_getSuperclass(classToSwizzle)
            };

            void (*msgSend)(struct objc_super *, SEL) = (__typeof__(msgSend))objc_msgSendSuper;
            msgSend(&superInfo, deallocSelector);
        } else {
            originalDealloc(objSelf, deallocSelector);
        }
    };

    IMP newDeallocIMP = imp_implementationWithBlock(newDealloc);

    if (!class_addMethod(classToSwizzle, deallocSelector, newDeallocIMP, "v@:")) {
        // The class already contains a method implementation.
        Method deallocMethod = class_getInstanceMethod(classToSwizzle, deallocSelector);

        // We need to store original implementation before setting new implementation
        // in case method is called at the time of setting.
        originalDealloc = (void(*)(__unsafe_unretained id, SEL))method_getImplementation(deallocMethod);

        // We need to store original implementation again, in case it just changed.
        originalDealloc = (void(*)(__unsafe_unretained id, SEL))method_setImplementation(deallocMethod, newDeallocIMP);
    }
}

请问各位大佬们有木有一套行之有效,无痛解决的方法....在线等,急!

PHP中文网PHP中文网2741 天前507

全部回复(1)我来回复

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:57:34

    你在pop的时候手动移除吧

    回复
    0
  • 取消回复