Peoples.m 991 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // Peoples.m
  3. // objc-test
  4. //
  5. // Created by huangyirong on 2020/4/3.
  6. //
  7. #import "Peoples.h"
  8. #import <objc/message.h>
  9. @implementation Peoples
  10. -(instancetype)init{
  11. self = [super init];
  12. if (self) {
  13. [self modifyProperty];
  14. }
  15. return self;
  16. }
  17. //2、获取类的属性列表-并修改属性值
  18. -(void)modifyProperty{
  19. NSLog(@"kkkk");
  20. unsigned int count = 0;
  21. Ivar *ivarList = class_copyIvarList(self.class, &count);
  22. for (NSUInteger i=0; i<count; i++) {
  23. Ivar ivar = ivarList[i];
  24. const char *ivarAddresss = ivar_getName(ivar);
  25. const char *ivarType = ivar_getTypeEncoding(ivar);
  26. NSString *address = [NSString stringWithUTF8String:ivarAddresss];
  27. NSLog(@"%d :ivarAddresss:%s ivarType:%s",i,ivarAddresss,ivarType);
  28. //修改属性值
  29. if ([address isEqualToString:@"_ivar_name"]) {
  30. object_setIvar(self, ivar, @"123456");
  31. }
  32. }
  33. // NSLog(@"_ivar_name:%@",_ivar_name);
  34. }
  35. @end