Runtime一 之获取对象的所有属性/方法

直接上代码

#import@implementation NSObject (XXOOProperty)

/* 获取对象的所有属性和属性内容 */

- (NSDictionary *)getAllPropertiesAndVaules
{
  NSMutableDictionary *props = [NSMutableDictionary dictionary];
  unsigned int outCount, i;
  objc_property_t *properties =class_copyPropertyList([self class], &outCount);
  for (i = 0; i<coutCount; i++)
{
  objc_property_t property = properties[i];
  const char* char_f =property_getName(property);
  NSString *propertyName = [NSString stringWithUTF8String:char_f];
  id propertyValue = [self valueForKey:(NSString *)propertyName];
  if (propertyValue) [props setObject:propertyValue forKey:propertyName];
}
  free(properties);
  return props;
}

/* 获取对象的所有属性 */

- (NSArray *)getAllProperties
{
  u_int count;
  objc_property_t *properties  =class_copyPropertyList([self class], &count);
  NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
  for (int i = 0; i < count ; i++)
  {
    const char* propertyName =property_getName(properties[i]);
    [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
  }
  free(properties);
  return propertiesArray;
}

/* 获取对象的所有方法 */

-(void)getAllMethods
{
  unsigned int mothCout_f =0;
  Method* mothList_f = class_copyMethodList([selfclass],&mothCout_f);
  for(int i=0;i<mothCout_f;i++)
{
  Method temp_f = mothList_f[i];
  IMP imp_f = method_getImplementation(temp_f);  
  SEL name_f = method_getName(temp_f);
  const char* name_s =sel_getName(method_getName(temp_f));
  int arguments = method_getNumberOfArguments(temp_f);
  const char* encoding =method_getTypeEncoding(temp_f);
  NSLog(@"方法名:%@,参数个数:%d,编码方式:%@",[NSString  stringWithUTF8String:name_s],
arguments,
  [NSString stringWithUTF8String:encoding]);
  }
  free(mothList_f);
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,150评论 6 13
  • 1.属性的简洁表示法 允许直接写入变量和函数 上面代码表明,ES6 允许在对象之中,直接写变量。这时,属性名为变量...
    雨飞飞雨阅读 1,153评论 0 3
  • 2017年的雪不知不觉飘落而至。 天有点冷。 时间也很快,过两天就要走了。心里还是有些许得不舍,想改签几天,貌似车...
    元气满满的慧慧酱阅读 218评论 0 1
  • 这是我朋友,记住啦,是朋友的故事! 不是我的哦!(哈哈,一丝丝欲盖弥彰的味道) 为了叙述方便,用第一人称说吧。只是...
    爱猩阅读 339评论 0 5
  • 《最后的告别》 这最后的告别没有声音 我孤零零一个人 呆呆的站在原地 落叶凋零 草木枯黄 你可看到我心底 昨日的...
    晚熟的柿子阅读 232评论 0 1