码迷,mamicode.com
首页 > 移动开发 > 详细

IOS高级开发~Runtime(二)

时间:2016-05-12 11:59:47      阅读:334      评论:0      收藏: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

IOS高级开发~Runtime(二)

标签:

原文地址:http://blog.csdn.net/flying_in_the_world/article/details/51371932

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!