Bez popisu

NSObject+ASToDictionary.m 877B

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // NSObject+ASToDictionary.m
  3. // ACSION
  4. //
  5. // Created by sunyue on 2019/4/24.
  6. // Copyright © 2019 acsion. All rights reserved.
  7. //
  8. #import "NSObject+ASToDictionary.h"
  9. @implementation NSObject (ASToDictionary)
  10. - (NSDictionary *)modelToDictionary
  11. {
  12. NSMutableDictionary *props = [NSMutableDictionary dictionary];
  13. unsigned int outCount, i;
  14. objc_property_t *properties = class_copyPropertyList([self class], &outCount);
  15. for (i = 0; i<outCount; i++)
  16. {
  17. objc_property_t property = properties[i];
  18. const char* char_f =property_getName(property);
  19. NSString *propertyName = [NSString stringWithUTF8String:char_f];
  20. id propertyValue = [self valueForKey:(NSString *)propertyName];
  21. if (propertyValue) [props setObject:propertyValue forKey:propertyName];
  22. }
  23. free(properties);
  24. return props;
  25. }
  26. @end