Category

作者: SDBridge | 来源:发表于2017-03-26 15:01 被阅读0次

    给类扩展方法

    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    @interface RuntimeCategoryClass:NSObject
    - (void)method1;
    @end
    @implementation RuntimeCategoryClass
    
    - (void)method1{
        
    }
    @end
    @interface RuntimeCategoryClass (Categroy)
    - (void)method2;
    @end
    
    
    @implementation RuntimeCategoryClass(Categroy)
    
    - (void)method2{
        
    }
    
    @end
    void test(){
    
    #pragma mark---
        NSLog(@"测试objc_class中的方法列表是否包含分类中得到方法");
        
        uint outcount = 0;
        
        Class cls  = RuntimeCategoryClass.class;
    
        Method *methodList  =class_copyMethodList(cls, &outcount);
        
        for (int i = 0 ; i<outcount; i++) {
            
            Method method = methodList[i];
            
            const char *name = sel_getName(method_getName(method));
            
            NSLog(@"RuntimeCategoryClass's method:%s",name);
            
            if (strcmp(name , sel_getName(@selector(method2)))) {
                
                NSLog(@"分类中的方法method2在objc_class的方法列表中");
            }
            
        }
        
        
     }
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
    
            test();
            
            // insert code here...
            NSLog(@"Hello, World!");
        }
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:Category

          本文链接:https://www.haomeiwen.com/subject/izlkottx.html