zoukankan      html  css  js  c++  java
  • 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{

            

        }

    }

      

  • 相关阅读:
    saxbuilder用法
    【转】开篇python--明白python文件如何组织,理解建立源文件
    [转]linux awk命令详解
    sed 指定行范围匹配(转)
    MySQL Error Code文档手册---摘自MySQL官方网站
    java文件读写操作大全
    详解coredump
    Java中Map根据键值(key)或者值(value)进行排序实现
    java如何对map进行排序详解(map集合的使用)
    遍历Map的四种方法
  • 原文地址:https://www.cnblogs.com/edensyd/p/8717332.html
Copyright © 2011-2022 走看看