IOS高级开发~Runtime(二)

#import <Foundation/Foundation.h>

@interface CustomClass : NSObject
{
    NSString *varTest1;
    NSString *varTest2;
    NSString *varTest3;
}
@property(nonatomic,strong) NSString *varTest1;
@property(nonatomic,strong) NSString *varTest2;
@property(nonatomic,strong) NSString *varTest3;
@interface CustomOtherClass : NSObject
{
    int varTest2;
}
-(void)fun2;

@interface ViewController (){

    float myFloat;

    CustomClass *allobj;

}

/**

 获取一个类所有方法

 */

-(void)getClassAllMethod{

    u_int count;

    Method *methods = class_copyMethodList([ViewController class], &count);

    for (int i = 0; i < count; i++) {

        SEL name = method_getName(methods[i]);

        NSString *strName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding];

        NSLog(@"strName:%@",strName);

    }

    

}

/**

 获取一个类的所有属性

 */

-(void)propertyNameList{

    u_int count;

    objc_property_t *properties = class_copyPropertyList([UIViewController class], &count);

    for (int i = 0; i < count; i++) {

        const char* propertyName = property_getName(properties[i]);

        NSString *strName = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];

        NSLog(@"strName:%@",strName);

    }

}

/**

 获取全局变量的值

 */

-(void)getInstanceVar{

    float myFloatValue;

    object_getInstanceVariable(self, "myFloat", (void*)&myFloatValue);

    NSLog(@"myFloatValue:%f",myFloatValue);

}

/**

 判断某一个类的某一个属性的类型

 */

-(void)getVarType{

    CustomClass *obj = [CustomClass new];

    Ivar var = class_getInstanceVariable(object_getClass(obj), "varTest1");

    const char*typeEdcoding = ivar_getTypeEncoding(var);

    NSString *strName = [NSString stringWithCString:typeEdcoding encoding:NSUTF8StringEncoding];

    if ([strName hasPrefix:@"@"]) {

        NSLog(@"handle class case");

    }else if ([strName hasPrefix:@"i"]){

        NSLog(@"handle int case");

    }else if ([strName hasPrefix:@"f"]){

        NSLog(@"handle float case");

    }else{

        

    }

}

 

  

posted @ 2018-04-04 15:07  心泪无恒  阅读(166)  评论(0编辑  收藏  举报