Rumah  >  Soal Jawab  >  teks badan

ios - Method Swizzing中一般替换方法都写在Category类别里吗?有没有别的实现方式

Method Swizzing中一般替换方法都写在Category类别里吗?有没有别的实现方式

PHP中文网PHP中文网2741 hari yang lalu526

membalas semua(1)saya akan balas

  • 高洛峰

    高洛峰2017-04-18 09:58:08

    Anda tidak perlu menulis kategori Anda boleh melaraskan kaedah berikut untuk bertukar-tukar kaedah.

    void methodSwizzle(Class c, SEL orig, SEL newS )
    {
        Method origMethod = class_getInstanceMethod(c, orig);
        Method newMethod = class_getInstanceMethod(c, newS);
        
        BOOL addSuccess = class_addMethod(c, orig, method_getImplementation(newMethod),method_getTypeEncoding(newMethod) );
        
        if (addSuccess) {
            class_replaceMethod(c, newS, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
            
        }else{
            method_exchangeImplementations(origMethod, newMethod);
            
        }
        
    }

    balas
    0
  • Batalbalas