归档普通对象Demo示例程序源代码
- 源代码下载链接:
06-归档普通对象.zip
34.2 KB // MJPerson.h
- //
- // MJPerson.h
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<Foundation/Foundation.h>
- @interfaceMJPerson : NSObject <NSCoding>
- @property(nonatomic,copy) NSString *name;
- @property(nonatomic,assign)intage;
- @property(nonatomic,assign)doubleheight;
- @end
// MJPerson.m
- //
- // MJPerson.m
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJPerson.h"
- @implementationMJPerson
- #pragma mark将对象归档的时候会调用(将对象写入文件之前会调用)
- //在这个方法说清楚:
- // 1.哪些属性需要存储
- // 2.怎样存储这些属性
- - (void)encodeWithCoder:(NSCoder *)encoder
- {
- //将_name属性值进行编码(会将_name的值存进文件)
- [encoder encodeObject:_name forKey:@"name"];
- [encoder encodeInt:_age forKey:@"age"];
- [encoder encodeDouble:_height forKey:@"height"];
- NSLog(@"Person---encodeWithCoder-----");
- }
- #pragma mark当从文件中解析对象时调用
- //在这个方法说清楚:
- // 1.哪些属性需要解析(读取)
- // 2.怎样解析(读取)这些属性
- - (id)initWithCoder:(NSCoder *)decoder
- {
- NSLog(@"Person---initWithCoder-----");
- if(self = [super init]) {
- _name = [decoder decodeObjectForKey:@"name"];
- _age = [decoder decodeIntForKey:@"age"];
- _height = [decoder decodeDoubleForKey:@"height"];
- }
- return self;
- }
- @end
// MJStudent.h
- //
- // MJStudent.h
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJPerson.h"
- @interfaceMJStudent : MJPerson
- @property(nonatomic,copy) NSString *no;
- @end
// MJStudent.m
- //
- // MJStudent.m
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJStudent.h"
- @implementation MJStudent
- - (void)encodeWithCoder:(NSCoder *)encoder
- {
- [super encodeWithCoder:encoder];
- [encoder encodeObject:_no forKey:@"no"];
- NSLog(@"MJStudent---encodeWithCoder-----");
- }
- //本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490624.html
- - (id)initWithCoder:(NSCoder *)decoder
- {
- if(self= [superinitWithCoder:decoder]) {
- NSLog(@"MJStudent---encodeWithCoder-----");
- _no = [decoder decodeObjectForKey:@"no"];
- }
- returnself;
- }
- @end
// MJViewController.h
- //
- // MJViewController.h
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<UIKit/UIKit.h>
- @interfaceMJViewController : UIViewController
- - (IBAction)save;
- - (IBAction)read;
- @end
// MJViewController.m
- //
- // MJViewController.m
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJViewController.h"
- #import"MJPerson.h"
- #import"MJStudent.h"
- @interfaceMJViewController ()
- @end
- @implementationMJViewController
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)didReceiveMemoryWarning
- {
- [superdidReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)save {
- // MJPerson *p = [[MJPerson alloc] init];
- // p.name = @"jack";
- // p.age = 10;
- // p.height = 1.55;
- //
- // NSString *path = @"/Users/apple/Desktop/person.data";
- // //归档
- // [NSKeyedArchiver archiveRootObject:p toFile:path];
- MJStudent *stu = [[MJStudent alloc] init];
- stu.name =@"rose";
- stu.age =20;
- stu.height =1.75;
- stu.no =@"110";
- NSString *path =@"/Users/apple/Desktop/person.data";
- [NSKeyedArchiver archiveRootObject:stu toFile:path];
- }
- - (IBAction)read {
- NSString *path =@"/Users/apple/Desktop/person.data";
- MJStudent *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
- NSLog(@"%@ - %d - %f- %@", stu.name, stu.age, stu.height, stu.no);
- ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490624.html
- // //读档(反归档)
- // MJPerson *p = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
- //
- // NSLog(@"%@ - %d - %f", p.name, p.age, p.height);
- }
- @end
归档普通对象Demo示例程序源代码的更多相关文章
- 03.WebView演练-iOS开发Demo(示例程序)源代码
技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong //转载请注明出处--本文永久链接:h ...
- iOS多线程
iOS开发Demo(示例程序)源代码
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版) iOS程序源代码下载链接:01.大任务.zip22 ...
- 代理设计模式iOS开发Demo(示例程序)源代码
iOS程序源代码下载链接:03-代理设计模式.zip28.3 KB // main.m // // main.m // 03-代理设计模式 // // Created by apple ...
- 01-QQ 3-最终重构版
Demo示例程序源代码
源代码下载链接:01-QQ 3.zip292.5 KB // QQAppDelegate.h Map // // QQAppDelegate.h // 01-QQ // // Created ...
- 01-导航实例-QQ空间Demo示例程序源代码
01-导航实例-QQ空间.zip62.4 KB // MJLoginViewController.h Map // // MJLoginViewController.h // 01-导航实例-QQ ...
- 01-modal
Demo示例程序源代码
源代码下载链接:01-modal.zip37.8 KB // MJAppDelegate.h // // MJAppDelegate.h // 01-modal // // Created by ...
- 02-更改窗口的根控制器
Demo示例程序源代码
源代码下载链接:02-更改窗口的根控制器.zip18.0 KB // MJAppDelegate.h // // MJAppDelegate.h // 02-更改窗口的根控制器 // // ...
- 12.13记录//QQDemo示例程序源代码
笔记的完整版pdf文档下载地址: https://www.evernote.com/shard/s227/sh/ac692160-68c7-4149-83ea-0db5385e28b0 ...
- kafka_2.11-0.8.2.1+java 生产消费程序demo示例
Kafka学习8_kafka java 生产消费程序demo示例 kafka是吞吐量巨大的一个消息系统,它是用scala写的,和普通的消息的生产消费还有所不同,写了个demo程序供大家参考.kaf ...
随机推荐
- java使用java.util.Properties读取properties文件的九种方法
直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...
- 激活Windows Server 2008R2
1. 用管理员身份运行mini-KMS_Activator_v1.053_ENG 2. 点击倒数第二个菜单Activation Windows VL 选择数字1 下一步选择Y 不管后面报不报错 3. ...
- AutoMapper.RegExtension 介绍
AutoMapper.RegExtension 为一个特小特小特小的用来根据约定自动调用AutoMapper中的方法配置映射的扩展库.你可以引入该库也可以将源码中核心部分的代码文件夹整个拷贝至项目中. ...
- Ubuntu16.04安装Zabbix
基于Zabbix+MySQL+Apache(可选) apt-get install php7.0-bcmath php7.0-xml php7.0-mbstring安装Zabbix所需的几个PHP模块 ...
- Unity和Lua交互
用lua就表示项目用到了热更新,通常每次热更新都会从服务器获取最新的lua脚本放到Android/ios设备的本地目录下,但是lua应该放到哪个目录下呢,这里就先说说lua里面的路径问题 1.不可以放 ...
- TP5 急速上手 语法规则
Tp5 规则 命名规范 目录和文件名采用‘小写+下划线’,并且以小写字母开头: 类库.函数文件统一以.php为后缀: 类的文件名均以命名空间定义,并且命名空间的路径和类库文件所在路径一致(包括大小写 ...
- 3.爬虫 urlib库讲解 总结
urllib库的总结: 用ProcessOn(安利这个软件,够用了)根据前面的几节内容做了个思维导图. urllib库一共有四个模块: request:它是最基本的模块,可以用来模拟发送请求 erro ...
- day02 智能合约
上午 1>部署智能合约网络 语法 require 2>利用第三方的节点 同步到以太坊 3>智能合约部署的步骤: 1.查看区块 2.发布合约 deploy后台经历的事情:就是部署合约的 ...
- Java中IO——NIO
一.引入 当引入一些新功能的时候,那说明之前的设计可能还需要完善. 1.阻塞式 在传统的IO输入输出中,如果我们从流中去读数据,而数据源中没有数据时,程序就会阻塞该线程.阻塞式线程的一种基本状态,可以 ...
- 【EasyNetQ】- 简介
EasyNetQ是一个简单易用的,稳定的的RabbitMQ .NET API . 如果您只想尽快启动并运行,请转到“ 快速开始”指南. EasyNetQ的目标是提供一个库,使得在.NET中使用Rabb ...