NSCoding 协议 父类只需要实现一次,所有子类 都可以 继承 的 runtime特性

来源:互联网 发布:已备案域名购买 编辑:程序博客网 时间:2024/06/05 20:28
  1. //  
  2. //  FYObject.m  
  3. //   
  4. //  
  5. //  Created by qiao.zhiguang on 15/5/5.  
  6. //  Copyright (c) 2015年  All rights reserved.  
  7. //  
  8.   
  9. #import "FYObject.h"  
  10. #import <objc/runtime.h>  
  11. @implementation FYObject  
  12.   
  13.   
  14. -(void)encodeWithCoder:(NSCoder *)encoder  
  15. {  
  16.     unsigned int count = 0;  
  17.     Ivar * ivars = class_copyIvarList([self class], &count);  
  18.       
  19.     for (int i = 0; i<count; i++) {  
  20.           
  21.         // 取出i位置对应的成员变量  
  22.         Ivar ivar = ivars[i];  
  23.           
  24.         // 查看成员变量  
  25.         const charchar *name = ivar_getName(ivar);  
  26.           
  27.         // 归档  
  28.         NSString *key = [NSString stringWithUTF8String:name];  
  29.         id value = [self valueForKey:key];  
  30.         [encoder encodeObject:value forKey:key];  
  31.     }  
  32.       
  33.     free(ivars);   
  34. }  
  35. -(id)initWithCoder:(NSCoder *)decoder  
  36. {  
  37.     if (self = [super init]) {  
  38.           
  39.         unsigned int count = 0;  
  40.         Ivar *ivars = class_copyIvarList([self class], &count);  
  41.           
  42.         for (int i = 0; i<count; i++) {  
  43.             // 取出i位置对应的成员变量  
  44.             Ivar ivar = ivars[i];  
  45.               
  46.             // 查看成员变量  
  47.             const charchar *name = ivar_getName(ivar);  
  48.               
  49.             // 归档  
  50.             NSString *key = [NSString stringWithUTF8String:name];  
  51.             id value = [decoder decodeObjectForKey:key];  
  52.               
  53.             // 设置到成员变量身上  
  54.             [self setValue:value forKey:key];  
  55.         }  
  56.           
  57.         free(ivars);  
  58.     }   
  59.     return self;   
  60. }  
  61.   
  62. - (NSString *)description{  
  63.     NSLog(@"%@ %s", [self class],__func__);  
  64.     NSMutableString * descriptionStr = [[NSMutableString alloc]init];  
  65.     unsigned int count = 0;  
  66.     Ivar *ivars = class_copyIvarList([self class], &count);  
  67.       
  68.     for (int i = 0; i<count; i++) {  
  69.         // 取出i位置对应的成员变量  
  70.         Ivar ivar = ivars[i];  
  71.         // 查看成员变量  
  72.         const charchar *name = ivar_getName(ivar);  
  73.         // 归档  
  74.         NSString *key = [NSString stringWithUTF8String:name];  
  75.         id value = [self valueForKey:key];  
  76.           
  77.         // 设置到成员变量身上  
  78.          [descriptionStr appendFormat:@"{%@:%@},",key,value];  
  79.     }  
  80.       
  81.     free(ivars);  
  82.     return descriptionStr;  
  83. }  
  84.   
  85. @end  

代码分享:

encodeWithCoder

大概逻辑,获取 当前类的 属性列表 以及 属性的 个数 count  ,然后遍历 所有 变量  取得 变量名 和  变量值  最后 进行 encode 

[encoder encodeObject:valueforKey:key];


  

initWithCoder

 获取当前类的属性列表,然后遍历,  根据 变量名   decode出 对应属性的 值 ,最后


[selfsetValue:valueforKey:key];


0 0
原创粉丝点击