RunTimeUtility.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // RunTimeModel.m
  3. // RunTimeThing
  4. //
  5. // Created by Linzhixiao on 16/4/15.
  6. // Copyright © 2016年 Linzhixiao. All rights reserved.
  7. //
  8. #import "RunTimeUtility.h"
  9. #import <objc/runtime.h>
  10. @implementation RunTimeUtility
  11. + (NSMutableArray *)getClassAllMemberProperty {
  12. unsigned int count;
  13. NSMutableArray *array = [NSMutableArray array];
  14. // 获取成员变量的数组的指针
  15. Ivar *ivars = class_copyIvarList([Person class], &count);
  16. for (int i = 0; i < count; i ++) {
  17. Ivar ivar = ivars[i];
  18. // 更加ivar获得其成员变量的名称
  19. const char *name = ivar_getName(ivar);
  20. // C的字符串转OC的字符串
  21. NSString *key = [NSString stringWithUTF8String:name];
  22. [array addObject:key];
  23. }
  24. free(ivars);
  25. return array;
  26. }
  27. + (NSMutableArray *)getClassAllPropertyName {
  28. unsigned int count;
  29. NSMutableArray *array = [NSMutableArray array];
  30. // 获得指向该类所有属性的指针
  31. objc_property_t *properties = class_copyPropertyList([Person class], &count);
  32. for (int i = 0; i < count; i ++) {
  33. // 获取该类的一个属性的指针
  34. objc_property_t property = properties[i];
  35. // 获取属性的名称
  36. const char *name = property_getName(property);
  37. // 转换
  38. NSString *key = [NSString stringWithUTF8String:name];
  39. [array addObject:key];
  40. }
  41. free(properties);
  42. return array;
  43. }
  44. + (NSMutableArray *)getClassAllMethod {
  45. unsigned int count;
  46. NSMutableArray *array = [NSMutableArray array];
  47. // 获取指向该类的所有方法的指针
  48. Method *methods = class_copyMethodList([Person class], &count);
  49. for (int i = 0; i < count; i ++) {
  50. // 获取该类的一个方法的指针
  51. Method method = methods[i];
  52. // 获取方法
  53. SEL methodSEL = method_getName(method);
  54. // 将方法转为C字符串
  55. const char *name = sel_getName(methodSEL);
  56. // 将C字符串转为OC字符串
  57. NSString *methodName = [NSString stringWithUTF8String:name];
  58. // 获取方法参数
  59. // int arguments = method_getNumberOfArguments(method);
  60. [array addObject:methodName];
  61. }
  62. free(methods);
  63. return array;
  64. }
  65. + (NSMutableArray *)getClassAllProtocolWithClass: (Class )className {
  66. unsigned int count;
  67. NSMutableArray *array = [NSMutableArray array];
  68. // 获取指向该类遵循的所有协议的数组指针
  69. __unsafe_unretained Protocol **protocols = class_copyProtocolList([className class], &count);
  70. for (int i = 0; i < count; i ++) {
  71. // 获取该类遵循的一个协议指针
  72. Protocol *protocol = protocols[i];
  73. // 获取C字符串协议名
  74. const char *name = protocol_getName(protocol);
  75. // C字符串转OC字符串
  76. NSString *protocolName = [NSString stringWithUTF8String:name];
  77. [array addObject:protocolName];
  78. }
  79. free(protocols);
  80. return array;
  81. }
  82. - (void)changeClassPropertyValueWithPersonObject: (Person *)person {
  83. unsigned int count = 0;
  84. Ivar *ivar = class_copyIvarList([Person class], &count);
  85. for (int i = 0; i < count; i ++) {
  86. Ivar var = ivar[i];
  87. const char *varName = ivar_getName(var);
  88. NSString *name = [NSString stringWithUTF8String:varName];
  89. if ([name isEqualToString:@"_name"]) {
  90. object_setIvar(person, var, @"张三");
  91. break;
  92. }
  93. }
  94. free(ivar);
  95. }
  96. + (void)exchangeClassMethod {
  97. Method m1= class_getInstanceMethod([Person class], @selector(sing));
  98. Method m2 = class_getInstanceMethod([Person class], @selector(drink));
  99. method_exchangeImplementations(m1, m2);
  100. }
  101. + (NSString *)addAndDoFromCityMethodWithPersonObject: (Person *)person {
  102. class_addMethod([Person class], @selector(fromCity:), (IMP)fromCityAnswer, "v@:@");
  103. if ([person respondsToSelector:@selector(fromCity:)]) {
  104. //Method method = class_getInstanceMethod([self.xiaoMing class], @selector(guess));
  105. [person performSelector:@selector(fromCity:) withObject:@"广州"];
  106. } else{
  107. NSLog(@"无法告诉你我从哪儿来");
  108. }
  109. return @"我来自广州";
  110. }
  111. void fromCityAnswer(id self,SEL _cmd,NSString *str){
  112. NSLog(@"我来自:%@",str);
  113. }
  114. @end