归档普通对象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 ...
随机推荐
- Spring MVC - URL路径映射
1. 普通映射 A. @RequestMapping("/test1") B. @RequestMapping(value={"/test1", "/ ...
- .NET基础知识之七——索引器
索引器是什么?有什么作用?索引器允许类的实例以访问数组的形式来访问对象里面的属性.如我们经常可以看到类似于dr["name"]="test",或者 ...
- (2)分布式下的爬虫Scrapy应该如何做-关于对Scrapy的反思和核心对象的介绍
本篇主要介绍对于一个爬虫框架的思考和,核心部件的介绍,以及常规的思考方法: 一,猜想 我们说的爬虫,一般至少要包含几个基本要素: 1.请求发送对象(sender,对于request的封装,防止被封) ...
- Qt 个性化标题栏,自定义标题栏
目前还没有达到自己满意的地步,魔方别人写的的,先提供参考,后面在加入新的东西 头文件 #ifndef TITLEBAR_H #define TITLEBAR_H #include <QWidge ...
- 虚拟现实-VR-UE4-创建第一个C++项目——Hello word
这部分主要是调用在C++中用代码实现在游戏界面上面输出一行文字 第一步,新建C++版本的工程文件,在4.12版本以后,在创建后,都会自动打开Vs编译器. 如下图 在VS中点击编译,等带编译,第一次等待 ...
- 九度OJ--1165(C++)
#include <iostream>#include <string>#include <vector> using namespace std; int mai ...
- Spring Data学习(一):初识
目录 前言 添加Spring Data 配置pom.xml 配置数据库相关信息(application.properties) 配置数据库信息 配置自动根据实体类在数据库创建表 创建User.java ...
- iOS-调用百度地图,苹果自带地图,高德地图,谷歌地图导航方法
- (void)actionSheet : (ServiceNetworkModel *)model{ __block NSString *urlScheme = @"demoURI://& ...
- (转)java中equals和等号(==)的区别浅谈
java中的数据类型,可分为两类:1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==) ...
- 【BZOJ2134】单选错位 概率DP
一句话:有一些看似有关系的期望在把事件全面发生之后就变得相互独立了 #include<cstdio> using namespace std; ]; double ans; int mai ...