検索

ホームページ  >  に質問  >  本文

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中文网2771日前527

全員に返信(1)返信します

  • 伊谢尔伦

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

    飛び出した場合は手動で削除できます

    返事
    0
  • キャンセル返事