1. 源代码下载链接:归档普通对象Demo示例程序源代码06-归档普通对象.zip
    34.2 KB
  2. //
  3. //  MJPerson.h
  4. //  06-归档普通对象
  5. //
  6. //  Created by apple on 13-12-11.
  7. //  Copyright (c) 2013itcast. All rights reserved.
  8. //

  9. #import<Foundation/Foundation.h>

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

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

  23. #import"MJPerson.h"

  24. @implementationMJPerson

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

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

  55. @end
  56. // MJStudent.h

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

  64. #import"MJPerson.h"

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

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

  76. #import"MJStudent.h"

  77. @implementation MJStudent

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

  95. @end
  96. // MJViewController.h

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

  104. #import<UIKit/UIKit.h>

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

  108. @end
  109. // MJViewController.m

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

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

  120. @interfaceMJViewController ()

  121. @end

  122. @implementationMJViewController

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

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

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

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

相关文章:

  • 2022-02-06
  • 2022-12-23
  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2021-04-05
相关资源
相似解决方案