归档普通对象Demo示例程序源代码

  1. 源代码下载链接:06-归档普通对象.zip
    34.2 KB
  2. // MJPerson.h

  3. //
  4. //  MJPerson.h
  5. //  06-归档普通对象
  6. //
  7. //  Created by apple on 13-12-11.
  8. //  Copyright (c) 2013itcast. All rights reserved.
  9. //

  10. #import<Foundation/Foundation.h>

  11. @interfaceMJPerson : NSObject <NSCoding>
  12. @property(nonatomic,copy) NSString *name;
  13. @property(nonatomic,assign)intage;
  14. @property(nonatomic,assign)doubleheight;
  15. @end
  16. // MJPerson.m

    Map
  17. //
  18. //  MJPerson.m
  19. //  06-归档普通对象
  20. //
  21. //  Created by apple on 13-12-11.
  22. //  Copyright (c) 2013itcast. All rights reserved.
  23. //

  24. #import"MJPerson.h"

  25. @implementationMJPerson

  26. #pragma mark将对象归档的时候会调用(将对象写入文件之前会调用)
  27. //在这个方法说清楚:
  28. // 1.哪些属性需要存储
  29. // 2.怎样存储这些属性
  30. - (void)encodeWithCoder:(NSCoder *)encoder
  31. {
  32.    //_name属性值进行编码(会将_name的值存进文件)
  33.     [encoder encodeObject:_name forKey:@"name"];
  34.    
  35.     [encoder encodeInt:_age forKey:@"age"];
  36.    
  37.    
  38.     [encoder encodeDouble:_height forKey:@"height"];
  39.    
  40.     NSLog(@"Person---encodeWithCoder-----");
  41. }

  42. #pragma mark当从文件中解析对象时调用
  43. //在这个方法说清楚:
  44. // 1.哪些属性需要解析(读取)
  45. // 2.怎样解析(读取)这些属性
  46. - (id)initWithCoder:(NSCoder *)decoder
  47. {
  48.     NSLog(@"Person---initWithCoder-----");
  49.    if(self = [super init]) {
  50.         _name = [decoder decodeObjectForKey:@"name"];
  51.         _age = [decoder decodeIntForKey:@"age"];
  52.         _height = [decoder decodeDoubleForKey:@"height"];
  53.     }
  54.    return self;
  55. }

  56. @end
  57. // MJStudent.h

    Map
  58. //
  59. //  MJStudent.h
  60. //  06-归档普通对象
  61. //
  62. //  Created by apple on 13-12-11.
  63. //  Copyright (c) 2013itcast. All rights reserved.
  64. //

  65. #import"MJPerson.h"

  66. @interfaceMJStudent : MJPerson
  67. @property(nonatomic,copy) NSString *no;
  68. @end
  69. // MJStudent.m

    Map
  70. //
  71. //  MJStudent.m
  72. //  06-归档普通对象
  73. //
  74. //  Created by apple on 13-12-11.
  75. //  Copyright (c) 2013itcast. All rights reserved.
  76. //

  77. #import"MJStudent.h"

  78. @implementation MJStudent

  79. - (void)encodeWithCoder:(NSCoder *)encoder
  80. {
  81.     [super encodeWithCoder:encoder];
  82.    
  83.     [encoder encodeObject:_no forKey:@"no"];
  84.    
  85.     NSLog(@"MJStudent---encodeWithCoder-----");
  86. }
  87. //本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490624.html
  88. - (id)initWithCoder:(NSCoder *)decoder
  89. {
  90.    if(self= [superinitWithCoder:decoder]) {
  91.         NSLog(@"MJStudent---encodeWithCoder-----");
  92.         _no = [decoder decodeObjectForKey:@"no"];
  93.     }
  94.    returnself;
  95. }

  96. @end
  97. // MJViewController.h

    Map
  98. //
  99. //  MJViewController.h
  100. //  06-归档普通对象
  101. //
  102. //  Created by apple on 13-12-11.
  103. //  Copyright (c) 2013itcast. All rights reserved.
  104. //

  105. #import<UIKit/UIKit.h>

  106. @interfaceMJViewController : UIViewController
  107. - (IBAction)save;
  108. - (IBAction)read;

  109. @end
  110. // MJViewController.m

    Map
  111. //
  112. //  MJViewController.m
  113. //  06-归档普通对象
  114. //
  115. //  Created by apple on 13-12-11.
  116. //  Copyright (c) 2013itcast. All rights reserved.
  117. //

  118. #import"MJViewController.h"
  119. #import"MJPerson.h"
  120. #import"MJStudent.h"

  121. @interfaceMJViewController ()

  122. @end

  123. @implementationMJViewController

  124. - (void)viewDidLoad
  125. {
  126.     [superviewDidLoad];
  127. // Do any additional setup after loading the view, typically from a nib.
  128. }

  129. - (void)didReceiveMemoryWarning
  130. {
  131.     [superdidReceiveMemoryWarning];
  132.    // Dispose of any resources that can be recreated.
  133. }

  134. - (IBAction)save {
  135. //    MJPerson *p = [[MJPerson alloc] init];
  136. //    p.name = @"jack";
  137. //    p.age = 10;
  138. //    p.height = 1.55;
  139. //   
  140. //    NSString *path = @"/Users/apple/Desktop/person.data";
  141. //    //归档
  142. //    [NSKeyedArchiver archiveRootObject:p toFile:path];
  143.    
  144.     MJStudent *stu = [[MJStudent alloc] init];
  145.     stu.name =@"rose";
  146.     stu.age =20;
  147.     stu.height =1.75;
  148.     stu.no =@"110";
  149.    
  150.     NSString *path =@"/Users/apple/Desktop/person.data";
  151.    
  152.     [NSKeyedArchiver archiveRootObject:stu toFile:path];
  153. }

  154. - (IBAction)read {
  155.     NSString *path =@"/Users/apple/Desktop/person.data";
  156.    
  157.     MJStudent *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
  158.    
  159.     NSLog(@"%@ - %d - %f- %@"stu.name, stu.age, stu.height, stu.no);
  160. ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490624.html
  161. //    //读档(反归档)
  162. //    MJPerson *p = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
  163. //   
  164. //    NSLog(@"%@ - %d - %f", p.name, p.age, p.height);
  165. }
  166. @end

你可能感兴趣的:(demo)