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,141評論 6 13
  • 1.屬性的簡潔表示法 允許直接寫入變量和函數 上面代碼表明,ES6 允許在對象之中,直接寫變量。這時,屬性名為變量...
    雨飛飛雨閱讀 1,147評論 0 3
  • 2017年的雪不知不覺飄落而至。 天有點冷。 時間也很快,過兩天就要走了。心里還是有些許得不舍,想改簽幾天,貌似車...
    元氣滿滿的慧慧醬閱讀 218評論 0 1
  • 這是我朋友,記住啦,是朋友的故事! 不是我的哦!(哈哈,一絲絲欲蓋彌彰的味道) 為了敘述方便,用第一人稱說吧。只是...
    愛猩閱讀 339評論 0 5
  • 《最后的告別》 這最后的告別沒有聲音 我孤零零一個人 呆呆的站在原地 落葉凋零 草木枯黃 你可看到我心底 昨日的...
    晚熟的柿子閱讀 232評論 0 1