YOUPAILCServiceInfo.m 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // YOUPAILCServiceInfo.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/4/11.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "YOUPAILCServiceInfo.h"
  9. @implementation YOUPAILCServiceInfo
  10. - (id) initWithCoder: (NSCoder *)decoder
  11. {
  12. if (self = [super init]) {
  13. unsigned int count = 0;
  14. //获取类中所有成员变量名
  15. Ivar *ivar = class_copyIvarList([YOUPAILCServiceInfo class], &count);
  16. for (int i = 0; i<count; i++) {
  17. Ivar iva = ivar[i];
  18. const char *name = ivar_getName(iva);
  19. NSString *strName = [NSString stringWithUTF8String:name];
  20. //进行解档取值
  21. id value = [decoder decodeObjectForKey:strName];
  22. //利用KVC对属性赋值
  23. if(value){
  24. [self setValue:value forKey:strName];
  25. }
  26. }
  27. free(ivar);
  28. }
  29. return self;
  30. }
  31. - (void) encodeWithCoder: (NSCoder *)encoder
  32. {
  33. unsigned int count;
  34. Ivar *ivar = class_copyIvarList([YOUPAILCServiceInfo class], &count);
  35. for (int i=0; i<count; i++) {
  36. Ivar iv = ivar[i];
  37. const char *name = ivar_getName(iv);
  38. NSString *strName = [NSString stringWithUTF8String:name];
  39. //利用KVC取值
  40. id value = [self valueForKey:strName];
  41. [encoder encodeObject:value forKey:strName];
  42. }
  43. free(ivar);
  44. }
  45. @end