您的位置:首页 > 移动开发 > IOS开发

iOS 获取类的属性,方法,属性类型

2017-02-13 14:17 330 查看
头文件中导入#import  <objc/runtime.h>

1. 获取类的属性

+(NSArray *)propertyList {

     unsigned int count = 0;

     objc_property_t *propertyList = class_copyPropertyList([self class] , &count);

     NSMutableArray *array = [NSMutableArray arrar];

     for(int i = 0 ; i < count ; i ++) {

          objc_property_t property = propertyList[i];

          const char *propertyChar = property_getName(property);

          NSString *propertyName = [NSString stringWithUTF8String:propertyName];

          [array addObject:propertyName];

     }

     free(propertyList);

     return array.copy;

}

2. 获取类的属性类型

+ (NSArray *)propertyType {

   unsigned int count = 0;

   objc_property_t *propertyList = class_copyPropertyList([self class] , &count);

   NSMutableArray *array = [NSMutableArray arrar];

    for(int i = 0 ; i < count ; i ++) {

          objc_property_t property = propertyList[i];

          const char *propertyChar = property_getAttributes(property);

          NSString *propertyName = [NSString stringWithUTF8String:propertyName];

          [array addObject:propertyName];

     }

     free(propertyList);

     return array.copy;
}

3.获取类的方法

+ (NSArray *)funcList {

    unsigned  int count = 0;

   Method *methodList = class_copyMethodList([self class] , &count);

  NSMutableArray *array = [NSMutableArray array];

  for(int i = 0 ; i < count ; i++) {

      Method method = methodList[i];

      SEL selector = method_getName(method);

      NSString *funcName  = NSStringWithSelector(selector);

      [array addObject:funcName];

  }

  free(methodList);

  return array.copy;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios