美文网首页
Runtime 那些事儿 二

Runtime 那些事儿 二

作者: David_Cap | 来源:发表于2015-08-25 09:19 被阅读471次

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

相关文章

  • Runtime那些事儿(消息机制)

    Runtime那些事儿(消息机制) Runtime那些事儿(消息机制)

  • Runtime 那些事儿 二

    1. 获取一个类的所有方法 2. 获取一个类的所有属性 3. 获取/设置类的属性变量 4. 判断类的某个属性的类型...

  • Runtime补救措施

    原文地址 :Runtime那些事儿(消息机制) 总结:当找不到对应的IML方法时,RunTime会尝试通过用户设定...

  • Runtime 那些事儿 总结

    引言 Objective-C 是一门动态的语言,这Runtime 是主要的功臣啊。下面我们举个?来初探一下 Run...

  • Runtime 那些事儿 一

    前言 为了方便测试,我们定义了一个测试类 1.对象拷贝:id object_copy(id obj, size_t...

  • Runtime那些事儿(消息机制)

    一、关于runtime 之前在项目中有遇到过用runtime解决改变全局字体的问题,所以再一次感受到了runtim...

  • Runtime那些事儿(消息机制)

    一、关于runtime 之前在项目中有遇到过用runtime解决改变全局字体的问题,所以再一次感受到了runtim...

  • Objective-C runtime那些事儿

    runtime 号称iOS开发的黑魔法。现在就让我来探究探究runtime,一来作为学习笔记,二来给有需要的人参考...

  • 汽车销售那些事儿15

    《汽车销售那些事儿》目录 汽车销售那些事儿16 汽车销售那些事儿14 第二天颜颜休息,理直气壮地不用早起,跟温柔两...

  • iOS--RunTime的那些事儿(一)

    引言:笔者在接触runtime之前,认为runtime是一个可用可不用的,因为在我们的实际开发中毕竟用的少。但是通...

网友评论

      本文标题:Runtime 那些事儿 二

      本文链接:https://www.haomeiwen.com/subject/pwkqcttx.html