首页 > 其他 > 详细

the runtime (二)

时间:2014-04-26 22:42:32      阅读:694      评论:0      收藏:0      [点我收藏+]

bubuko.com,布布扣

这两个函数,一个是拷贝一个新的对象,一个是释放对象,但都不支持ARC。。。。

 

  OBJC_EXPORT Class object_setClass(id obj, Class cls) 

       __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

改变成新的class并返回旧的class

 

OBJC_EXPORT const char *object_getClassName(id obj)

    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

获取class

 

 

bubuko.com,布布扣
OBJC_EXPORT const char *object_getClassName(id obj)

    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

获取类名,不过是const char


bubuko.com,布布扣

 

bubuko.com,布布扣
 向一个类动态添加方法

 BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)

 参数说明:

 cls:被添加方法的类

 name:可以理解为方法名

 imp:实现这个方法的函数

 types:一个定义该函数返回值类型和参数类型的字符串

 class_addMethod([TestClass class], @selector(ocMethod:), (IMP)testFunc, "i@:@");

 

 其中types参数为"i@:@“,按顺序分别表示:

 i:返回值类型int,若是v则表示void

 @:参数id(self)

 ::SEL(_cmd)

 @:id(str)
bubuko.com,布布扣

 

bubuko.com,布布扣
获取一个类的所有方法
- (void) getClassAllMethod { u_int count; Method* methods= class_copyMethodList([UIViewController 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); } }
bubuko.com,布布扣

 

bubuko.com,布布扣
获取一个类的所有属性:
- (void) propertyNameList
{
    u_int count;
    objc_property_t *properties=class_copyPropertyList([UIViewControllerclass], &count);//取属性列表
    for (int i = 0; i < count ; i++)
    {
        const char* propertyName =property_getName(properties[i]);//取属性名
        NSString *strName = [NSString stringWithCString:propertyNameencoding:NSUTF8StringEncoding];
        NSLog(@"%@",strName);
    }
}
bubuko.com,布布扣
bubuko.com,布布扣
获取/设置类的属性变量
//获取全局变量的值   (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);
}
bubuko.com,布布扣
bubuko.com,布布扣
判断类的某个属性的类型
- (void) getVarType {
    ClassCustomClass *obj = [ClassCustomClass new];
    Ivar var = class_getInstanceVariable(object_getClass(obj),"varTest1"); //这的两个参数,一个是对象,一个是属性的名字(字符串)
    const char* typeEncoding =ivar_getTypeEncoding(var);
    NSString *stringType =  [NSStringstringWithCString:typeEncodingencoding:NSUTF8StringEncoding];
    
    if ([stringType hasPrefix:@"@"]) {
        // 如果是对象
        NSLog(@"handle class case");
    } else if ([stringTypehasPrefix:@"i"]) {
        // 整型
        NSLog(@"handle int case");
    } else if ([stringTypehasPrefix:@"f"]) {
        // 浮点
        NSLog(@"handle float case");
    } else
    {
 
    }
}
bubuko.com,布布扣
bubuko.com,布布扣
通过属性的值来获取其属性的名字(反射机制)
- (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);//获取全部的Ivar
    
    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;
}
bubuko.com,布布扣

 

bubuko.com,布布扣
系统类的方法实现部分替换
- (void) methodExchange {
    Method m1 = class_getInstanceMethod([NSString class],@selector(lowercaseString));
    Method m2 = class_getInstanceMethod([NSString class],@selector(uppercaseString));
    method_exchangeImplementations(m1, m2);//方法名给换了,小写变大写,大写变小写
    NSLog(@"%@", [@"sssAAAAss" lowercaseString]);
    NSLog(@"%@", [@"sssAAAAss" uppercaseString]);
}
bubuko.com,布布扣
bubuko.com,布布扣
自定义类的方法实现部分替换
- (void) justLog1 {
    NSLog(@"justLog1");
}
- (void) justLog2 {
    NSLog(@"justLog2");
}
- (void) methodSetImplementation {
    Method method = class_getInstanceMethod([ClassMethodViewCtr class],@selector(justLog1));
    IMP originalImp = method_getImplementation(method);(关于IMP不明白的看(一))
    Method m1 = class_getInstanceMethod([ClassMethodViewCtr class],@selector(justLog2));
    method_setImplementation(m1, originalImp);//替换justLog2,相当于改变了justLog2的函数指针,前面的是被替换的参数,后面的替换的参数
}
在执行justLog2方法,实际执行的是justLog1
bubuko.com,布布扣

 

the runtime (二),布布扣,bubuko.com

the runtime (二)

原文:http://www.cnblogs.com/lingzhiguiji/p/3692035.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!