美文网首页iOS学习笔记
iOS获取某类的属性/方法/成员变量/协议列表

iOS获取某类的属性/方法/成员变量/协议列表

作者: 见哥哥长高了 | 来源:发表于2018-12-18 16:17 被阅读6次

需要用到:

#import <objc/runtime.h>

在这里我们随便找一个控制器

@interface ViewController ()

@property(nonatomic,strong)NSString *name;

@end

//获取属性列表

    unsigned int number;
    objc_property_t *propertiList = class_copyPropertyList([self class], &number);
    for (unsigned int i = 0; i < number; i++) {
        const char *propertyName = property_getName(propertiList[i]);
        NSLog(@"属性名称----%@",[NSString stringWithUTF8String:propertyName]);
    }
    free(propertiList);
属性列表

//获取方法列表

    unsigned int methodCount;
    Method *method = class_copyMethodList([self class], &methodCount);
    for (unsigned int i = 0; i < methodCount; i++) {
        Method me = method[i];
        NSLog(@"方法名称----%@",NSStringFromSelector(method_getName(me)));
    }
    free(method);
方法列表

//获取成员变量列表

    unsigned int ivarCount;
    Ivar *ivar = class_copyIvarList([self class], &ivarCount);
    for (NSInteger index = 0; index < ivarCount; index++) {
        const char *ivarName = ivar_getName(ivar[index]);
        NSLog(@"成员变量名称----%@",[NSString stringWithUTF8String:ivarName]);
    }
    free(ivar);
成员变量

//获取协议列表


屏幕快照 2018-12-18 下午4.15.40.png

首先我们先遵守协议
@interface ViewController ()<NSCoding>

    unsigned int protocalCount;
    //获取协议列表
    __unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &protocalCount);
    for (unsigned int i = 0; i< protocalCount; i++) {
        Protocol *myProtocal = protocolList[i];
        const char *protocolName = protocol_getName(myProtocal);
        NSLog(@"协议名称----%@", [NSString stringWithUTF8String:protocolName]);
    }
    free(protocolList);
协议列表

相关文章

  • iOS获取某类的属性/方法/成员变量/协议列表

    需要用到: 在这里我们随便找一个控制器 //获取属性列表 //获取方法列表 //获取成员变量列表 //获取协议列表...

  • ios中runtime 笔记

    常见方法 1.获取属性列表 2.获取方法列表 3,获取成员变量列表 4,获取协议列表 5,获得类方法

  • Runtime使用总结

    本文主要内容有:获取属性/方法/协议/成员变量列表、动态关联属性、动态添加方法、方法交换。 一、获取列表 使用Ru...

  • RunTime 相关函数使用

    方法交换,一般在分类的load方法使用 获取方法列表 获取实例变量列表 获取实例属性列表 获取协议列表 为类别添加...

  • Runtime 整理

    做iOS 的基本上没有有几个不知道Runtime的吧; 1.通过runtime 获取成员变量列表、属性列表、方法列...

  • iOS-runtime使用总结

    关联 获取类名 获取属性列表(公有和私有) 获取成员变量 修改对象指针 方法交换(Method Swizzling...

  • iOS中Runtime常用示例

    Runtime的内容大概有:动态获取类名、动态获取类的成员变量、动态获取类的属性列表、动态获取类的方法列表、动态获...

  • iOS-Runtime

    Runtime的内容大概有:动态获取类名、动态获取类的成员变量、动态获取类的属性列表、动态获取类的方法列表、动态获...

  • Category-分类

    ios中的对象类型分为三类: 实例对象:存放成员变量的具体指;类对象:存放对象方法,属性,成员变量,协议信息等;元...

  • runtime基础

    目前我所了解的Runtime内容大约有:动态获取类名、动态获取类的成员变量、动态获取类的属性列表、动态获取类的方法...

网友评论

    本文标题:iOS获取某类的属性/方法/成员变量/协议列表

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