美文网首页
runtime 获取类的所有属性,方法以及isa

runtime 获取类的所有属性,方法以及isa

作者: 充满活力的早晨 | 来源:发表于2018-04-26 18:28 被阅读13次

    获取类的所有属性

    这里不讲解怎么使用,只是单纯的用代码解析了下类属性。方便以后查找方便

    #import <Foundation/Foundation.h>
    
    @interface ObjcPropertyParse : NSObject
    - (instancetype)initWithClass:(Class)cls;
    -(void)print;
    @end
    
    #import <Foundation/Foundation.h>
    #import "Son.h"
    struct personStruct{
        int man ;
    };
    
    @interface Person : NSObject
    @property (nonatomic,assign) struct personStruct structSon;
    @property (nonatomic,strong) NSString *name;
    @property (nonatomic,retain) NSString *name1;
    @property (nonatomic,copy) NSString *name2;
    @property (atomic,copy) NSString *name3;
    @property (nonatomic,strong) NSString *name4;
    @property (nonatomic,weak) NSString *name5;
    @property (nonatomic,getter=isProxy) int mm;
    @property (nonatomic,setter=isdd:) int mmd;
    @property (nonatomic,strong) Son  *son;
    @property (nonatomic,strong) NSMutableArray *address;
    -(void)addAddress:(NSString *)address;
    @end
    
    
    
    #import "Person.h"
    #import <objc/runtime.h>
    #import "ObjcClassMethodParse.h"
    @implementation Person
    @dynamic name4;
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
       
        }
        return self;
    }
    
    @end
    
    
    #import <Foundation/Foundation.h>
    
    @interface ObjcPropertyParse : NSObject
    - (instancetype)initWithClass:(Class)cls;
    -(void)print;
    @end
    
    
    #import "ObjcPropertyParse.h"
    #import <objc/runtime.h>
    
    
    
    
    @interface ObjcProperty: NSObject
    @property (nonatomic,strong) NSString *property;
    @property(nonatomic ,assign) bool isRetain;
    @property(nonatomic ,assign) bool isCopy;
    @property(nonatomic ,assign) bool isNonatomic;
    @property(nonatomic ,assign) NSString * isGetter;
    @property(nonatomic ,assign) NSString * isSetter;
    @property(nonatomic ,assign) bool isdynamic;
    @property(nonatomic ,assign) bool isWeak;
    @property(nonatomic ,assign) bool isP;
    @property(nonatomic ,assign) NSString  * isT;
    @property (nonatomic,strong) NSString *ivar;
    @property (nonatomic ,strong)NSString * propertyName;
    
    @end
    
    @implementation ObjcProperty
    
    @end
    
    @interface ObjcPropertyParse()
    @property (nonatomic,strong) Class cls;
    @property (nonatomic,strong) NSMutableArray * propertyList;
    @end
    
    @implementation ObjcPropertyParse
    - (instancetype)initWithClass:(Class)cls
    {
        self = [super init];
        if (self) {
            self.cls = cls;
            self.propertyList = [NSMutableArray array];
            [self parse];
           
        }
        return self;
    }
    //https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW1
    -(void)parse{
    
        unsigned int count = 0;
        objc_property_t *property_t = class_copyPropertyList(self.cls, &count);
        for (int i=0; i<count; i++) {
            objc_property_t propert = property_t[i];
            const char * propertyName = property_getName(propert);
    
        unsigned int attcount = 0;
           objc_property_attribute_t *att = property_copyAttributeList(propert,&attcount);
            ObjcProperty * objc=[[ObjcProperty alloc]init];
            objc.propertyName = [NSString stringWithFormat:@"%s",propertyName];
            for (int j =0; j<attcount; j++) {
                objc_property_attribute_t t = att[j];
                if (strcmp(t.name,"T")==0) {
                    objc.property = [NSString stringWithFormat:@"%s",t.value];
                }
                if (strcmp(t.name,"&")==0) {
                    objc.isRetain = YES;
                }
                if (strcmp(t.name,"&")==0) {
                    objc.isRetain = YES;
                }
                if (strcmp(t.name,"N")==0) {
                    objc.isNonatomic = YES;
                }
                if (strcmp(t.name,"D")==0) {
                    objc.isdynamic = YES;
                }
                if (strcmp(t.name,"G")==0) {
                    objc.isGetter =  [NSString stringWithFormat:@"%s",t.value];
                }
                if (strcmp(t.name,"S")==0) {
                    objc.isSetter =  [NSString stringWithFormat:@"%s",t.value];
                }
                if (strcmp(t.name,"W")==0) {
                    objc.isWeak = YES;
                }
                
                if (strcmp(t.name,"P")==0) {
                    objc.isP = YES;
                }
                if (strcmp(t.name,"t")==0) {
                    objc.isT =  [NSString stringWithFormat:@"%s",t.value];;
                }
                if (strcmp(t.name,"V")==0) {
                    objc.ivar =  [NSString stringWithFormat:@"%s",t.value];;
    
                }
            }
            [self.propertyList addObject:objc];
        
        }
        free(property_t);
    
    }
    
    -(void)print{
        for (ObjcProperty * objcProperty in self.propertyList) {
            NSLog(@" propertyName=%@,property=%@,isCopy=%d,isRetain=%d,isNonatomic=%d,isGetter=%@,isSetting=%@,isDynamic=%d,isWeak=%d,isP=%d,isT=%@,Ivar=%@",objcProperty.propertyName,objcProperty.property,objcProperty.isCopy,objcProperty.isRetain,objcProperty.isNonatomic,objcProperty.isGetter,objcProperty.isSetter,objcProperty.isdynamic,objcProperty.isWeak,objcProperty.isWeak,objcProperty.isT,objcProperty.ivar);
        
        
        }
    }
    
    
    @end
    

    调用测试

        ObjcPropertyParse * objcPropertyParse = [[ObjcPropertyParse alloc]initWithClass:[Person class]];
        [objcPropertyParse print];
    

    结果

    2018-04-26 14:26:33.206827+0800 RuntimeParseClass[19146:1786992]  propertyName=structSon,property={personStruct=i},isCopy=0,isRetain=0,isNonatomic=1,isGetter=(null),isSetting=(null),isDynamic=0,isWeak=0,isP=0,isT=(null),Ivar=_structSon
    2018-04-26 14:26:33.206962+0800 RuntimeParseClass[19146:1786992]  propertyName=name,property=@"NSString",isCopy=0,isRetain=1,isNonatomic=1,isGetter=(null),isSetting=(null),isDynamic=0,isWeak=0,isP=0,isT=(null),Ivar=_name
    2018-04-26 14:26:33.207067+0800 RuntimeParseClass[19146:1786992]  propertyName=name1,property=@"NSString",isCopy=0,isRetain=1,isNonatomic=1,isGetter=(null),isSetting=(null),isDynamic=0,isWeak=0,isP=0,isT=(null),Ivar=_name1
    2018-04-26 14:26:33.207162+0800 RuntimeParseClass[19146:1786992]  propertyName=name2,property=@"NSString",isCopy=0,isRetain=0,isNonatomic=1,isGetter=(null),isSetting=(null),isDynamic=0,isWeak=0,isP=0,isT=(null),Ivar=_name2
    2018-04-26 14:26:33.207290+0800 RuntimeParseClass[19146:1786992]  propertyName=name3,property=@"NSString",isCopy=0,isRetain=0,isNonatomic=0,isGetter=(null),isSetting=(null),isDynamic=0,isWeak=0,isP=0,isT=(null),Ivar=_name3
    2018-04-26 14:26:33.207707+0800 RuntimeParseClass[19146:1786992]  propertyName=name4,property=@"NSString",isCopy=0,isRetain=1,isNonatomic=1,isGetter=(null),isSetting=(null),isDynamic=1,isWeak=0,isP=0,isT=(null),Ivar=(null)
    2018-04-26 14:26:33.207855+0800 RuntimeParseClass[19146:1786992]  propertyName=name5,property=@"NSString",isCopy=0,isRetain=0,isNonatomic=1,isGetter=(null),isSetting=(null),isDynamic=0,isWeak=1,isP=1,isT=(null),Ivar=_name5
    2018-04-26 14:26:33.208059+0800 RuntimeParseClass[19146:1786992]  propertyName=mm,property=i,isCopy=0,isRetain=0,isNonatomic=1,isGetter=isProxy,isSetting=(null),isDynamic=0,isWeak=0,isP=0,isT=(null),Ivar=_mm
    2018-04-26 14:26:33.208252+0800 RuntimeParseClass[19146:1786992]  propertyName=mmd,property=i,isCopy=0,isRetain=0,isNonatomic=1,isGetter=(null),isSetting=isdd:,isDynamic=0,isWeak=0,isP=0,isT=(null),Ivar=_mmd
    2018-04-26 14:26:33.208780+0800 RuntimeParseClass[19146:1786992]  propertyName=son,property=@"Son",isCopy=0,isRetain=1,isNonatomic=1,isGetter=(null),isSetting=(null),isDynamic=0,isWeak=0,isP=0,isT=(null),Ivar=_son
    2018-04-26 14:26:33.208912+0800 RuntimeParseClass[19146:1786992]  propertyName=address,property=@"NSMutableArray",isCopy=0,isRetain=1,isNonatomic=1,isGetter=(null),isSetting=(null),isDynamic=0,isWeak=0,isP=0,isT=(null),Ivar=_address
    
    

    这里主要是知道属性的编码规范,查看官方文档

    image.png

    方法类的所有方法

    这里只获取下类,并且打印出类的所有方法和相关属性,不讨论IMP交换什么的

    #import <Foundation/Foundation.h>
    
    @interface ObjcClassMethodParse : NSObject
    - (instancetype)initWithClass:(Class)cls;
    -(void)print;
    
    @end
    
    
    #import "ObjcClassMethodParse.h"
    #import <objc/runtime.h>
    
    
    
    @interface ObjcClassMethod: NSObject
    @property (nonatomic,strong) NSString *name;
    @property (nonatomic,assign) IMP imp;
    @property (nonatomic,strong) NSString *ypeEncoding;
    @property (nonatomic,assign) int  argumentCount;
    @property (nonatomic,strong) NSString * cReturnType;
    @property (nonatomic,strong) NSMutableArray *cArgumentTypes;
    @property (nonatomic,strong) NSString * getReturnType;
    @property (nonatomic,strong) NSMutableArray *gArgumentTypes;
    
    @property (nonatomic,strong) NSString * descriptionName;
    @property (nonatomic,strong) NSString * descriptionType;
    
    @end
    
    @implementation ObjcClassMethod
    
    @end
    
    @interface ObjcClassMethodParse()
    @property (nonatomic,strong) Class cls;
    @property (nonatomic,strong) NSMutableArray * methodListArr;
    @end
    @implementation ObjcClassMethodParse
    - (instancetype)initWithClass:(Class)cls
    {
        self = [super init];
        if (self) {
            self.cls = cls;
            self.methodListArr = [NSMutableArray array];
            [self parse];
            
        }
        return self;
    }
    //https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW1
    -(void)parse{
        
        unsigned int count = 0;
        Method *methodList = class_copyMethodList(self.cls, &count);
        for (int i=0; i<count; i++) {
            Method method = methodList[i];
            ObjcClassMethod * m=[[ObjcClassMethod alloc]init];
         const char * name= sel_getName(method_getName(method));
            
            m.name = [NSString stringWithFormat:@"%s",name];
            IMP imp= method_getImplementation(method);
            m.imp = imp;
          const char *ypeEncoding =  method_getTypeEncoding(method);
            m.ypeEncoding = [NSString stringWithFormat:@"%s",ypeEncoding];
           int count = method_getNumberOfArguments(method);
            m.argumentCount = count;
          const char *returnType =  method_copyReturnType(method);
            m.cReturnType = [NSString stringWithFormat:@"%s",returnType];
            NSMutableArray * argumentstype= [NSMutableArray array];
            for (int i=0; i<count; i++) {
              char * argumentType=  method_copyArgumentType(method, i);
                [argumentstype addObject:[NSString stringWithFormat:@"%s",argumentType]];
            }
            m.cArgumentTypes =argumentstype;
            char  dst[1024]={0};
            method_getReturnType(method,dst , 1024);
            m.getReturnType =[NSString stringWithFormat:@"%s",dst];
            
            NSMutableArray * gargumentstype= [NSMutableArray array];
    
            for (int i=0; i<count; i++) {
                char  dst[1024]={0};
                method_getArgumentType(method, i, dst, 1024);
                [gargumentstype addObject:[NSString stringWithFormat:@"%s",dst]];
            }
            m.gArgumentTypes = gargumentstype;
           
            struct objc_method_description * de=   method_getDescription(method);
            m.descriptionName = NSStringFromSelector(de->name);
            m.descriptionType =[NSString stringWithFormat:@"%s", de->types];
            [self.methodListArr addObject:m];
        }
        free(methodList);
        
    }
    
    -(void)print{
        for (ObjcClassMethod *m in self.methodListArr) {
            NSString * str = [NSString stringWithFormat:@"name=%@, imp=%p ,typeEncoding=%@ ,argumentCount=%d,cReturnType=%@,getReturnType=%@,descriptionName=%@,descriptionType=%@,cArgumentTypes=%@,gArgumentTypes=%@",m.name ,m.imp,m.ypeEncoding,m.argumentCount,m.cReturnType,m.getReturnType,m.descriptionType,m.descriptionType,[m.cArgumentTypes componentsJoinedByString:@"|"],[m.gArgumentTypes componentsJoinedByString:@"|"]];
            NSLog(@"%@",str);
            
            
        }
    }
    
    @end
    
    

    测试代码

     ObjcClassMethodParse * methodParse =[[ObjcClassMethodParse alloc]initWithClass:[Person class]];
        [methodParse print];
    

    测试结果

    2018-04-26 15:06:20.660783+0800 RuntimeParseClass[29459:1870748] name=structSon, imp=0x10c811570 ,typeEncoding={personStruct=i}16@0:8 ,argumentCount=2,cReturnType={personStruct=i},getReturnType={personStruct=i},descriptionName={personStruct=i}16@0:8,descriptionType={personStruct=i}16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.661063+0800 RuntimeParseClass[29459:1870748] name=setStructSon:, imp=0x10c8115a0 ,typeEncoding=v20@0:8{personStruct=i}16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v20@0:8{personStruct=i}16,descriptionType=v20@0:8{personStruct=i}16,cArgumentTypes=@|:|{personStruct=i},gArgumentTypes=@|:|{personStruct=i}
    2018-04-26 15:06:20.661199+0800 RuntimeParseClass[29459:1870748] name=name1, imp=0x10c811630 ,typeEncoding=@16@0:8 ,argumentCount=2,cReturnType=@,getReturnType=@,descriptionName=@16@0:8,descriptionType=@16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.661303+0800 RuntimeParseClass[29459:1870748] name=setName1:, imp=0x10c811650 ,typeEncoding=v24@0:8@16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v24@0:8@16,descriptionType=v24@0:8@16,cArgumentTypes=@|:|@,gArgumentTypes=@|:|@
    2018-04-26 15:06:20.661413+0800 RuntimeParseClass[29459:1870748] name=name2, imp=0x10c811690 ,typeEncoding=@16@0:8 ,argumentCount=2,cReturnType=@,getReturnType=@,descriptionName=@16@0:8,descriptionType=@16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.831869+0800 RuntimeParseClass[29459:1870748] name=setName2:, imp=0x10c8116c0 ,typeEncoding=v24@0:8@16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v24@0:8@16,descriptionType=v24@0:8@16,cArgumentTypes=@|:|@,gArgumentTypes=@|:|@
    2018-04-26 15:06:20.832085+0800 RuntimeParseClass[29459:1870748] name=name3, imp=0x10c811700 ,typeEncoding=@16@0:8 ,argumentCount=2,cReturnType=@,getReturnType=@,descriptionName=@16@0:8,descriptionType=@16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.832218+0800 RuntimeParseClass[29459:1870748] name=setName3:, imp=0x10c811730 ,typeEncoding=v24@0:8@16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v24@0:8@16,descriptionType=v24@0:8@16,cArgumentTypes=@|:|@,gArgumentTypes=@|:|@
    2018-04-26 15:06:20.832337+0800 RuntimeParseClass[29459:1870748] name=name5, imp=0x10c811770 ,typeEncoding=@16@0:8 ,argumentCount=2,cReturnType=@,getReturnType=@,descriptionName=@16@0:8,descriptionType=@16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.864817+0800 RuntimeParseClass[29459:1870748] name=setName5:, imp=0x10c8117b0 ,typeEncoding=v24@0:8@16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v24@0:8@16,descriptionType=v24@0:8@16,cArgumentTypes=@|:|@,gArgumentTypes=@|:|@
    2018-04-26 15:06:20.864973+0800 RuntimeParseClass[29459:1870748] name=setMm:, imp=0x10c811810 ,typeEncoding=v20@0:8i16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v20@0:8i16,descriptionType=v20@0:8i16,cArgumentTypes=@|:|i,gArgumentTypes=@|:|i
    2018-04-26 15:06:20.865089+0800 RuntimeParseClass[29459:1870748] name=mmd, imp=0x10c811840 ,typeEncoding=i16@0:8 ,argumentCount=2,cReturnType=i,getReturnType=i,descriptionName=i16@0:8,descriptionType=i16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.865192+0800 RuntimeParseClass[29459:1870748] name=isdd:, imp=0x10c811860 ,typeEncoding=v20@0:8i16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v20@0:8i16,descriptionType=v20@0:8i16,cArgumentTypes=@|:|i,gArgumentTypes=@|:|i
    2018-04-26 15:06:20.865310+0800 RuntimeParseClass[29459:1870748] name=son, imp=0x10c811890 ,typeEncoding=@16@0:8 ,argumentCount=2,cReturnType=@,getReturnType=@,descriptionName=@16@0:8,descriptionType=@16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.865424+0800 RuntimeParseClass[29459:1870748] name=setSon:, imp=0x10c8118b0 ,typeEncoding=v24@0:8@16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v24@0:8@16,descriptionType=v24@0:8@16,cArgumentTypes=@|:|@,gArgumentTypes=@|:|@
    2018-04-26 15:06:20.865528+0800 RuntimeParseClass[29459:1870748] name=address, imp=0x10c8118f0 ,typeEncoding=@16@0:8 ,argumentCount=2,cReturnType=@,getReturnType=@,descriptionName=@16@0:8,descriptionType=@16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.865708+0800 RuntimeParseClass[29459:1870748] name=.cxx_destruct, imp=0x10c811950 ,typeEncoding=v16@0:8 ,argumentCount=2,cReturnType=v,getReturnType=v,descriptionName=v16@0:8,descriptionType=v16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.865884+0800 RuntimeParseClass[29459:1870748] name=name, imp=0x10c8115d0 ,typeEncoding=@16@0:8 ,argumentCount=2,cReturnType=@,getReturnType=@,descriptionName=@16@0:8,descriptionType=@16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.866053+0800 RuntimeParseClass[29459:1870748] name=setName:, imp=0x10c8115f0 ,typeEncoding=v24@0:8@16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v24@0:8@16,descriptionType=v24@0:8@16,cArgumentTypes=@|:|@,gArgumentTypes=@|:|@
    2018-04-26 15:06:20.866225+0800 RuntimeParseClass[29459:1870748] name=init, imp=0x10c8114e0 ,typeEncoding=@16@0:8 ,argumentCount=2,cReturnType=@,getReturnType=@,descriptionName=@16@0:8,descriptionType=@16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.866461+0800 RuntimeParseClass[29459:1870748] name=isProxy, imp=0x10c8117f0 ,typeEncoding=i16@0:8 ,argumentCount=2,cReturnType=i,getReturnType=i,descriptionName=i16@0:8,descriptionType=i16@0:8,cArgumentTypes=@|:,gArgumentTypes=@|:
    2018-04-26 15:06:20.866655+0800 RuntimeParseClass[29459:1870748] name=setAddress:, imp=0x10c811910 ,typeEncoding=v24@0:8@16 ,argumentCount=3,cReturnType=v,getReturnType=v,descriptionName=v24@0:8@16,descriptionType=v24@0:8@16,cArgumentTypes=@|:|@,gArgumentTypes=@|:|@
    

    相关文章

      网友评论

          本文标题:runtime 获取类的所有属性,方法以及isa

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