首页 > 代码库 > IOS高级开发~Runtime(二)

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 =http://www.mamicode.com/ 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

 

IOS高级开发~Runtime(二)