zoukankan      html  css  js  c++  java
  • IOS高级开发~Runtime(二)

    6、获取一个类的所有方法:

    u_int count;

            Method * methods = class_copyMethodList([CustomClass 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);

            }

    运行结果:

    2014-10-20 11:57:27.884 runtimetest[10939:303] fun1

    2014-10-20 11:57:27.886 runtimetest[10939:303] fun3

    2014-10-20 11:57:27.886 runtimetest[10939:303] varTest1

    2014-10-20 11:57:27.886 runtimetest[10939:303] setVarTest1:

    2014-10-20 11:57:27.887 runtimetest[10939:303] varTest2

    2014-10-20 11:57:27.887 runtimetest[10939:303] setVarTest2:

    2014-10-20 11:57:27.887 runtimetest[10939:303] varTest3

    2014-10-20 11:57:27.888 runtimetest[10939:303] setVarTest3:

    7

    //获取一个类的所有属性

            u_int count;

            objc_property_t * properties = class_copyPropertyList([CustomClass 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);

            }

    运行结果:

    2014-10-20 12:01:23.508 runtimetest[11167:303] varTest1

    2014-10-20 12:01:23.510 runtimetest[11167:303] varTest2

    2014-10-20 12:01:23.510 runtimetest[11167:303] varTest3

    8、获取/设置类的属性变量

    //获取全局变量的值   (myFloat 为类的一个属性变量)

    - (void) getInstanceVar {

        float myFloatValue;

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

        NSLog(@"%f", myFloatValue);

    }

    //设置全局变量的值

    - (void) setInstanceVar {

        float newValue = 10.00f;

        unsigned int addr = (unsignedint)&newValue;

        object_setInstanceVariable(self,"myFloat", *(float**)addr);

        NSLog(@"%f", myFloat);

    }

    9、判断类的某个属性的类型

    - (void) getVarType {

        ClassCustomClass *obj = [ClassCustomClassnew];

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

        const char* typeEncoding =ivar_getTypeEncoding(var);

        NSString *stringType =  [NSStringstringWithCString:typeEncodingencoding:NSUTF8StringEncoding];

        

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

            // handle class case

            NSLog(@"handle class case");

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

            // handle int case

            NSLog(@"handle int case");

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

            // handle float case

            NSLog(@"handle float case");

        } else

        {

     

        }

    }

     

    10、通过属性的值来获取其属性的名字(反射机制)

    - (NSString *)nameOfInstance:(id)instance

    {

        unsigned int numIvars =0;

        NSString *key=nil;

        

        //Describes the instance variables declared by a class. 

        Ivar * ivars = class_copyIvarList([ClassCustomClassclass], &numIvars);

        

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

            Ivar thisIvar = ivars[i];

            

            const char *type =ivar_getTypeEncoding(thisIvar);

            NSString *stringType =  [NSStringstringWithCString:typeencoding:NSUTF8StringEncoding];

            

            //不是class就跳过

            if (![stringType hasPrefix:@"@"]) {

                continue;

            }

            

            //Reads the value of an instance variable in an object. object_getIvar这个方法中,当遇到非objective-c对象时,并直接crash

            if ((object_getIvar(allobj, thisIvar) == instance)) {

                // Returns the name of an instance variable.

                key = [NSStringstringWithUTF8String:ivar_getName(thisIvar)];

                break;

            }

        }

        free(ivars);

        return key;

    }

    测试代码:

     

    allobj = [ClassCustomClassnew];

        allobj.varTest1 =@"varTest1String";

        allobj.varTest2 =@"varTest2String";

        allobj.varTest3 =@"varTest3String";

        NSString *str = [selfnameOfInstance:@"varTest1String"];

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

     

     

    打印结果:

    2013-07-26 16:26:26.271 HighOC[7081:c07] str:varTest1

     

  • 相关阅读:
    Go安装
    Redis 安装与使用
    scala总结
    C++学习笔记4
    LeetCode 22.将数组分成和相等的三个部分
    LeetCode 21.二叉树的直径 DFS深度遍历
    LeetCode 20.买卖股票的最佳时机 暴力破解法与动态规划
    LeetCode 19.凑零钱问题 动态规划
    LeetCode 18.队列的最大值
    Java SSM Spring MVC 三层架构和MVC+SpringMVC的入门案例+请求参数的绑定+常用的注解
  • 原文地址:https://www.cnblogs.com/q403154749/p/4037175.html
Copyright © 2011-2022 走看看