zoukankan      html  css  js  c++  java
  • IOS高级开发~Runtime(二)

    #import <Foundation/Foundation.h>
    
    @interface CustomClass : NSObject
    {
        NSString *varTest1;
        NSString *varTest2;
        NSString *varTest3;
    }
    @property(nonatomic,strong) NSString *varTest1;
    @property(nonatomic,strong) NSString *varTest2;
    @property(nonatomic,strong) NSString *varTest3;
    
    @interface CustomOtherClass : NSObject
    {
        int varTest2;
    }
    -(void)fun2;

    @interface ViewController (){

        float myFloat;

        CustomClass *allobj;

    }

    /**

     获取一个类所有方法

     */

    -(void)getClassAllMethod{

        u_int count;

        Method *methods = class_copyMethodList([ViewController 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:%@",strName);

        }

        

    }

    /**

     获取一个类的所有属性

     */

    -(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:%@",strName);

        }

    }

    /**

     获取全局变量的值

     */

    -(void)getInstanceVar{

        float myFloatValue;

        object_getInstanceVariable(self, "myFloat", (void*)&myFloatValue);

        NSLog(@"myFloatValue:%f",myFloatValue);

    }

    /**

     判断某一个类的某一个属性的类型

     */

    -(void)getVarType{

        CustomClass *obj = [CustomClass new];

        Ivar var = class_getInstanceVariable(object_getClass(obj), "varTest1");

        const char*typeEdcoding = ivar_getTypeEncoding(var);

        NSString *strName = [NSString stringWithCString:typeEdcoding encoding:NSUTF8StringEncoding];

        if ([strName hasPrefix:@"@"]) {

            NSLog(@"handle class case");

        }else if ([strName hasPrefix:@"i"]){

            NSLog(@"handle int case");

        }else if ([strName hasPrefix:@"f"]){

            NSLog(@"handle float case");

        }else{

            

        }

    }

      

  • 相关阅读:
    eclipse真机调试显示Target unknown的解决方法
    教你看懂GERBER中的钻孔(.txt)文件
    Quartus ii 12.0 和ModelSim 10.1 SE安装及连接
    Android的学习——ubuntu下android5.1源码的make编译
    ubuntu 14.04 下找不到命令,路径出错
    【转载】VMware虚拟机修改硬盘容量大小
    Fedora10下建立linux系统的窗口没有地址栏
    [转]SecureCRT连接主机时,无法从键盘输入
    在FASTBuild中使用Distribution
    在FASTBuild中使用Caching
  • 原文地址:https://www.cnblogs.com/edensyd/p/8717332.html
Copyright © 2011-2022 走看看