Runtime 那些事儿 二

2015-08-25  本文已影响471人  David_Cap

1. 获取一个类的所有方法

- (void) getClassAllMethod
{
    u_int count;
    Method* methods= class_copyMethodList([TestClass 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);
    }
}


说明:获取TestClass中的所以方法的列表,count表示 方法的数量计数器。Method中就是一个放着所以方法的数组

2. 获取一个类的所有属性

- (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);
    }
}

说明:和上面的类似

3. 获取/设置类的属性变量

//获取全局变量的值   (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);
}

4. 判断类的某个属性的类型

- (void) getVarType {
    TestClass *obj = [[TestClass alloc]init];
    Ivar var = class_getInstanceVariable(object_getClass(obj),"varTest1");
    const char* typeEncoding =ivar_getTypeEncoding(var);
    NSString *stringType =  [NSStringstringWithCString:typeEncoding encoding: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
    {
 
    }
}

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

- (NSString *)nameOfInstance:(id)instance
{
    unsigned int numIvars =0;
    NSString *key=nil;
    
    //Describes the instance variables declared by a class. 
    Ivar * ivars = class_copyIvarList([TestClass new], &numIvars);
    
    for(int i = 0; i < numIvars; i++) {
        Ivar thisIvar = ivars[i];
        
        const char *type =ivar_getTypeEncoding(thisIvar);
        NSString *stringType =  [NSStringstringWithCString:type encoding: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;
}

参考致谢

http://blog.csdn.net/lizhongfu2013/article/details/9497187

上一篇 下一篇

猜你喜欢

热点阅读