Runtime(二)

23 篇文章 0 订阅

一些公用类:

@interface ClassCustomClass :NSObject{

    NSString *varTest1;

    NSString *varTest2;

    NSString *varTest3;

}

@property (nonatomic,assign)NSString *varTest1;

@property (nonatomic,assign)NSString *varTest2;

@property (nonatomic,assign)NSString *varTest3;

- (void) fun1;

@end

@implementation ClassCustomClass

@synthesize varTest1, varTest2, varTest3;

- (void) fun1 {

    NSLog(@"fun1");

}

@end


@interface ClassCustomClassOther :NSObject {

    int varTest2;

}

- (void) fun2;

@end


@implementation ClassCustomClassOther

- (void) fun2 {

    NSLog(@"fun2");

}

@end


@interface ClassPropertyViewCtr () {

    float myFloat;

    ClassCustomClass *allobj;

}

myFloat = 2.34f;


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

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

    }

}

打印结果(部分):

2013-07-26 16:07:03.972 HighOC[7021:c07] _screen

2013-07-26 16:07:03.973 HighOC[7021:c07] applicationWillSuspend

2013-07-26 16:07:03.973 HighOC[7021:c07] _tryBecomeRootViewControllerInWindow:

2013-07-26 16:07:03.973 HighOC[7021:c07] isViewLoaded

2013-07-26 16:07:03.974 HighOC[7021:c07] view

......................

7、获取一个类的所有属性

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

    }

}

打印结果(部分)

2013-07-26 16:09:42.182 HighOC[7041:c07] tabBarItem

2013-07-26 16:09:42.184 HighOC[7041:c07] tabBarController

2013-07-26 16:09:42.185 HighOC[7041:c07] splitViewController

2013-07-26 16:09:42.186 HighOC[7041:c07] navigationItem

2013-07-26 16:09:42.186 HighOC[7041:c07] hidesBottomBarWhenPushed

...............

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值