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) 2013年itcast. 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) 2013年itcast. 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) 2013年itcast. 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) 2013年itcast. 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) 2013年itcast. 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) 2013年itcast. 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示例程序源代码的更多相关文章

  1. 03.WebView演练-iOS开发Demo(示例程序)源代码

    技术博客http://www.cnblogs.com/ChenYilong/   新浪微博http://weibo.com/luohanchenyilong   //转载请注明出处--本文永久链接:h ...

  2. iOS多线程 iOS开发Demo(示例程序)源代码

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版)   iOS程序源代码下载链接:01.大任务.zip22 ...

  3. 代理设计模式iOS开发Demo(示例程序)源代码

        iOS程序源代码下载链接:03-代理设计模式.zip28.3 KB // main.m // //  main.m //  03-代理设计模式 // //  Created by apple ...

  4. 01-QQ 3-最终重构版 Demo示例程序源代码

      源代码下载链接:01-QQ 3.zip292.5 KB // QQAppDelegate.h Map // //  QQAppDelegate.h //  01-QQ // //  Created ...

  5. 01-导航实例-QQ空间Demo示例程序源代码

    01-导航实例-QQ空间.zip62.4 KB // MJLoginViewController.h Map // //  MJLoginViewController.h //  01-导航实例-QQ ...

  6. 01-modal Demo示例程序源代码

    源代码下载链接:01-modal.zip37.8 KB // MJAppDelegate.h // //  MJAppDelegate.h //  01-modal // //  Created by ...

  7. 02-更改窗口的根控制器 Demo示例程序源代码

      源代码下载链接:02-更改窗口的根控制器.zip18.0 KB // MJAppDelegate.h // //  MJAppDelegate.h //  02-更改窗口的根控制器 // //  ...

  8. 12.13记录//QQDemo示例程序源代码

            笔记的完整版pdf文档下载地址: https://www.evernote.com/shard/s227/sh/ac692160-68c7-4149-83ea-0db5385e28b0 ...

  9. kafka_2.11-0.8.2.1+java 生产消费程序demo示例

      Kafka学习8_kafka java 生产消费程序demo示例 kafka是吞吐量巨大的一个消息系统,它是用scala写的,和普通的消息的生产消费还有所不同,写了个demo程序供大家参考.kaf ...

随机推荐

  1. Windows2008新建域时Administrator 帐户密码不符合要求

             Windows 2008 系统安装完毕后,(环境:在安装的时间,系统没有设置密码.做好系统后,进入制面板添加了密码或按ctrl + alt + del 设置密码后 在服务器管理-角色 ...

  2. Qt Qwdget 汽车仪表知识点拆解1 速度表示

    先贴上效果图,注意,没有写逻辑,所以这些都是乱动的 这里线主要说一下中间显示速度的显示制作的方式,在这里,自己专门写了一个数字的仪表 考虑的一般的汽车是没有办法把瞬时速度提升到四位数的,所以我这里就放 ...

  3. Qt 解析网络数据出现ssl错误

    最近写了点小东西,哈哈, 网络部分是同学帮我搞的 在编译的时候,出现了一下错误 qt.network.ssl: QSslSocket: cannot call unresolved function ...

  4. SpriteKit游戏开发适配iPad/iPhone6/7/8/Plus及iPhoneX的尺寸及安全区域

    未适配前:Ball球超过屏幕的上下方   适配后:Ball球就在屏幕的可视范围内运动了 一.那么如何适配不同的iPhone.iPhoneX及iPad的屏幕尺寸呢? 我们开发一个App的时候, 通常希望 ...

  5. spring mvc:实现给Controller函数传入list<pojo>参数

    [1]前端js调用示例: ...insertStatisData?statisDatas=[{'cid':'2','devId':'9003','deviceName':'测试名','endTime' ...

  6. neutron DVR

    DVR 简介 DVR 提出的背景 在 Neutron 的网络环境中,跨子网的虚机通信是需要通过 Neutron 的路由器.这既包括不同子网的虚拟机之间的通信,又包括虚拟机与外网之间的通信.在 DVR ...

  7. thinkphp3.2 验证码的使用

    验证码生成: public function verify(){ ob_clean(); $verify = new \Think\Verify; $verify->codeSet = '012 ...

  8. C++ 点

    2017/12/23 scoped_ptr类型的指针,只能在一个namespace中使用 1) 怎么查看元素在set中是否存在 1) istringstream类用于执行C++风格的串流的输入操作. ...

  9. React & styled component

    React & styled component https://www.styled-components.com/#your-first-styled-component tagged t ...

  10. 如何获取iframe DOM的值

    在Web开发时,很多时候会遇到一个问题.我在一个页面嵌入了iframe,并且我想获得这个iframe页面某个元素的值.那么该如何实现这个需求呢? 先来看下演示: 效果演示 iframe1中文本框的值: ...