• 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{

            

        }

    }

      

  • 相关阅读:
    Spring+Mybatis配置
    Python 字符串
    图像处理知识相关
    CUDA编程学习相关
    Pthon学习相关
    【论文阅读】ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices
    【pytorch】pytorch学习笔记(一)
    【论文阅读】MEAL: Multi-Model Ensemble via Adversarial Learning
    【论文阅读】HydraPlus-Net: Attentive Deep Features for Pedestrian Analysis
    【论文阅读】Learning Spatial Regularization with Image-level Supervisions for Multi-label Image Classification
  • 原文地址:https://www.cnblogs.com/edensyd/p/8717332.html
Copyright © 2020-2023  润新知