NSObject+FIDProperty.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // NSObject+FIDProperty.m
  3. // runtimeCategory
  4. //
  5. // Created by Fidetro on 2016/11/4.
  6. // Copyright © 2016年 Fidetro. All rights reserved.
  7. //
  8. #import "NSObject+FIDProperty.h"
  9. @implementation NSObject (FIDProperty)
  10. + (NSArray *)propertyOfSelf{
  11. unsigned int count = 0;
  12. Ivar *ivarList = class_copyIvarList(self, &count);
  13. NSMutableArray *propertyNames = [NSMutableArray array];
  14. for (int i = 0; i < count; i++)
  15. {
  16. Ivar ivar = ivarList[i];
  17. NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
  18. NSString *key;
  19. if ([[name substringToIndex:1] isEqualToString:@"_"])
  20. {
  21. key = [name substringFromIndex:1];
  22. }
  23. else
  24. {
  25. key = [name substringFromIndex:0];
  26. }
  27. [propertyNames addObject:key];
  28. }
  29. free(ivarList);
  30. return [propertyNames copy];
  31. }
  32. - (NSArray *)getPublicObject
  33. {
  34. NSMutableArray *objectArray = [NSMutableArray array];
  35. unsigned int outCount;
  36. Ivar * ivars = class_copyIvarList([self class], &outCount);
  37. for (int i = 0; i < outCount; i ++) {
  38. Ivar ivar = ivars[i];
  39. [objectArray addObject:object_getIvar(self, ivar)];
  40. }
  41. return [objectArray copy];
  42. }
  43. - (NSDictionary *)modelToDictionary
  44. {
  45. NSArray *propertyNames = [[self class] propertyOfSelf];
  46. NSMutableDictionary *objectParams = [NSMutableDictionary dictionary];
  47. for (NSString *propertyName in propertyNames)
  48. {
  49. if ([[self sendGetMethodWithPropertyName:propertyName] class] == nil)
  50. {
  51. [objectParams setObject:@"" forKey:propertyName];
  52. }
  53. else
  54. {
  55. [objectParams setObject:[self sendGetMethodWithPropertyName:propertyName] forKey:propertyName];
  56. }
  57. }
  58. return [objectParams copy];
  59. }
  60. - (NSDictionary *)modelToDictionaryHaveNilProperty
  61. {
  62. NSArray *propertyNames = [[self class] propertyOfSelf];
  63. NSMutableDictionary *objectParams = [NSMutableDictionary dictionary];
  64. for (NSString *propertyName in propertyNames)
  65. {
  66. if ([[self sendGetMethodWithPropertyName:propertyName] class] == nil)
  67. {
  68. continue;
  69. }
  70. [objectParams setObject:[self sendGetMethodWithPropertyName:propertyName] forKey:propertyName];
  71. }
  72. return [objectParams copy];
  73. }
  74. - (NSArray *)modelToArray
  75. {
  76. NSArray *propertyNames = [[self class] propertyOfSelf];
  77. NSMutableArray *objectArray = [NSMutableArray array];
  78. for (NSString *propertyName in propertyNames)
  79. {
  80. if ([[self sendGetMethodWithPropertyName:propertyName] class] == nil)
  81. {
  82. [objectArray addObject:@""];
  83. }
  84. else
  85. {
  86. [objectArray addObject:[self sendGetMethodWithPropertyName:propertyName]];
  87. }
  88. }
  89. return [objectArray copy];
  90. }
  91. - (NSDictionary *)modelToArrayHaveNilProperty
  92. {
  93. NSArray *propertyNames = [[self class] propertyOfSelf];
  94. NSMutableArray *objectArray = [NSMutableArray array];
  95. for (NSString *propertyName in propertyNames)
  96. {
  97. if ([[self sendGetMethodWithPropertyName:propertyName] class] == nil)
  98. {
  99. continue;
  100. }
  101. else
  102. {
  103. [objectArray addObject:[self sendGetMethodWithPropertyName:propertyName]];
  104. }
  105. }
  106. return [objectArray copy];
  107. }
  108. - (id)sendGetMethodWithPropertyName:(NSString *)propertyName{
  109. // SEL getSel = NSSelectorFromString(propertyName);
  110. // if ([self respondsToSelector:getSel]) {
  111. // return [self performSelector:getSel];
  112. // }
  113. // return nil;
  114. return [self valueForKey:propertyName];
  115. }
  116. - (void)setPropertyWithName:(NSString *)propertyName object:(id)object{
  117. // NSString *firstCharater = [propertyName substringToIndex:1].uppercaseString;
  118. // NSString *setPropertyName = [NSString stringWithFormat:@"set%@%@:",firstCharater,[propertyName substringFromIndex:1]];
  119. // SEL setSel = NSSelectorFromString(setPropertyName);
  120. // [self performSelector:setSel withObject:object];
  121. [self setValue:object forKey:propertyName];
  122. }
  123. @end