iOS开发UI篇—ios应用数据存储方式(归档) :转发
本文转发至:文顶顶http://www.cnblogs.com/wendingding/p/3775293.html
iOS开发UI篇—ios应用数据存储方式(归档)
一、简单说明
二、代码示例
1.文件结构

2.代码示例
YYViewController.m文件

1 //
2 // YYViewController.m
3 // 02-归档
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10 #import "YYPerson.h"
11
12 @interface YYViewController ()
13 - (IBAction)saveBtnOnclick:(id)sender;
14 - (IBAction)readBtnOnclick:(id)sender;
15
16 @end
17
18 @implementation YYViewController
19
20 - (void)viewDidLoad
21 {
22 [super viewDidLoad];
23 }
24
25
26 - (IBAction)saveBtnOnclick:(id)sender {
27 //1.创建对象
28 YYPerson *p=[[YYPerson alloc]init];
29 p.name=@"文顶顶";
30 p.age=23;
31 p.height=1.7;
32
33 //2.获取文件路径
34 NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
35 NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
36 NSLog(@"path=%@",path);
37
38 //3.将自定义的对象保存到文件中
39 [NSKeyedArchiver archiveRootObject:p toFile:path];
40
41 }
42
43 - (IBAction)readBtnOnclick:(id)sender {
44 //1.获取文件路径
45 NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
46 NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
47 NSLog(@"path=%@",path);
48
49 //2.从文件中读取对象
50 YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
51 NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
52 }
53 @end

新建一个person类
YYPerson.h文件

1 //
2 // YYPerson.h
3 // 02-归档
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 // 如果想将一个自定义对象保存到文件中必须实现NSCoding协议
12 @interface YYPerson : NSObject<NSCoding>
13
14 //姓名
15 @property(nonatomic,copy)NSString *name;
16 //年龄
17 @property(nonatomic,assign)int age;
18 //身高
19 @property(nonatomic,assign)double height;
20 @end

YYPerson.m文件

1 //
2 // YYPerson.m
3 // 02-归档
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYPerson.h"
10
11 @implementation YYPerson
12
13 // 当将一个自定义对象保存到文件的时候就会调用该方法
14 // 在该方法中说明如何存储自定义对象的属性
15 // 也就说在该方法中说清楚存储自定义对象的哪些属性
16 -(void)encodeWithCoder:(NSCoder *)aCoder
17 {
18 NSLog(@"调用了encodeWithCoder:方法");
19 [aCoder encodeObject:self.name forKey:@"name"];
20 [aCoder encodeInteger:self.age forKey:@"age"];
21 [aCoder encodeDouble:self.height forKey:@"height"];
22 }
23
24 // 当从文件中读取一个对象的时候就会调用该方法
25 // 在该方法中说明如何读取保存在文件中的对象
26 // 也就是说在该方法中说清楚怎么读取文件中的对象
27 -(id)initWithCoder:(NSCoder *)aDecoder
28 {
29 NSLog(@"调用了initWithCoder:方法");
30 //注意:在构造方法中需要先初始化父类的方法
31 if (self=[super init]) {
32 self.name=[aDecoder decodeObjectForKey:@"name"];
33 self.age=[aDecoder decodeIntegerForKey:@"age"];
34 self.height=[aDecoder decodeDoubleForKey:@"height"];
35 }
36 return self;
37 }
38 @end

3.打印效果和两个重要的错误提示
点击保存按钮和读取按钮,成功打印结果如下:

关于不实现两个协议方法的错误提示:
-(void)encodeWithCoder:(NSCoder *)aCoder方法:

-(id)initWithCoder:(NSCoder *)aDecoder方法:

三、继承类中的使用
新建一个学生类,让这个类继承自Preson这个类,增加一个体重的属性。
YYstudent.h文件

1 //
2 // YYstudent.h
3 // 02-归档
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYPerson.h"
10
11 @interface YYstudent : YYPerson
12 //增加一个体重属性
13 @property(nonatomic,assign) double weight;
14 @end

YYstudent.m文件

1 //
2 // YYstudent.m
3 // 02-归档
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYstudent.h"
10
11 @implementation YYstudent
12
13 //在子类中重写这两个方法
14 - (void)encodeWithCoder:(NSCoder *)aCoder
15 {
16 [super encodeWithCoder:aCoder];
17 NSLog(@"调用了YYStudent encodeWithCoder");
18 [aCoder encodeFloat:self.weight forKey:@"weight"];
19 }
20
21 - (id)initWithCoder:(NSCoder *)aDecoder
22 {
23 if (self = [super initWithCoder:aDecoder]) {
24 NSLog(@"调用了YYstudent initWithCoder");
25 self.weight = [aDecoder decodeFloatForKey:@"weight"];
26 }
27 return self;
28 }
29 @end

YYViewController.m文件

1 //
2 // YYViewController.m
3 // 02-归档
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10 #import "YYPerson.h"
11 #import "YYstudent.h"
12
13 @interface YYViewController ()
14 - (IBAction)saveBtnOnclick:(id)sender;
15 - (IBAction)readBtnOnclick:(id)sender;
16
17 @end
18
19 @implementation YYViewController
20
21 - (void)viewDidLoad
22 {
23 [super viewDidLoad];
24 }
25
26
27 - (IBAction)saveBtnOnclick:(id)sender {
28 //1.创建对象
29 // YYPerson *p=[[YYPerson alloc]init];
30 // p.name=@"文顶顶";
31 // p.age=23;
32 // p.height=1.7;
33
34 YYstudent *s=[[YYstudent alloc]init];
35 s.name=@"wendingding";
36 s.age=23;
37 s.height=1.7;
38 s.weight=62;
39 //2.获取文件路径
40 NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
41 NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
42 NSLog(@"path=%@",path);
43
44 //3.将自定义的对象保存到文件中
45 // [NSKeyedArchiver archiveRootObject:p toFile:path];
46 [NSKeyedArchiver archiveRootObject:s toFile:path];
47
48 }
49
50 - (IBAction)readBtnOnclick:(id)sender {
51 //1.获取文件路径
52 NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
53 NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
54 NSLog(@"path=%@",path);
55
56 //2.从文件中读取对象
57 // YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
58 // NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
59 YYstudent *s=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
60 NSLog(@"%@,%d,%.1f,%f",s.name,s.age,s.height,s.weight);
61 }
62 @end

点击保存按钮和读取按钮后的打印输出:

四、重要说明
1.保存数据过程:

//1.创建对象
YYstudent *s=[[YYstudent alloc]init];
s.name=@"wendingding";
s.age=23;
s.height=1.7;
s.weight=62; //2.获取文件路径
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path); //3.将自定义的对象保存到文件中
[NSKeyedArchiver archiveRootObject:s toFile:path];

2.读取数据过程:
//1.获取文件路径
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
//2.从文件中读取对象
YYstudent *s=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
3.遵守NSCoding协议,并实现该协议中的两个方法。
4.如果是继承,则子类一定要重写那两个方法。因为person的子类在存取的时候,会去子类中去找调用的方法,没找到那么它就去父类中找,所以最后保存和读取的时候新增加的属性会被忽略。需要先调用父类的方法,先初始化父类的,再初始化子类的。
5.保存数据的文件的后缀名可以随意命名。
6.通过plist保存的数据是直接显示的,不安全。通过归档方法保存的数据在文件中打开是乱码的,更安全。
iOS开发UI篇—ios应用数据存储方式(归档) :转发的更多相关文章
- iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)
iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存 ...
- iOS开发UI篇—ios应用数据存储方式(偏好设置)
iOS开发UI篇—ios应用数据存储方式(偏好设置) 一.简单介绍 很多iOS应用都支持偏好设置,比如保存用户名.密码.字体大小等设置,iOS提供了一套标准的解决方案来为应用加入偏好设置功能 每个应用 ...
- iOS开发UI篇—ios应用数据存储方式(归档)
iOS开发UI篇—ios应用数据存储方式(归档) 一.简单说明 在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦: 偏好设置(将所有的东西都保存在同 ...
- iOS开发UI篇—iOS开发中三种简单的动画设置
iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...
- iOS开发UI篇—IOS开发中Xcode的一些使用技巧
iOS开发UI篇—IOS开发中Xcode的一些使用技巧 一.快捷键的使用 经常用到的快捷键如下: 新建 shift + cmd + n 新建项目 cmd + n 新建文 ...
- iOS开发UI篇—无限轮播(新闻数据展示)
iOS开发UI篇—无限轮播(新闻数据展示) 一.实现效果 二.实现步骤 1.前期准备 (1)导入数据转模型的第三方框架MJExtension (2)向项目中添加保存有“新闻”数据的pli ...
- iOS开发UI篇—字典转模型
iOS开发UI篇—字典转模型 一.能完成功能的“问题代码” 1.从plist中加载的数据 2.实现的代码 // // LFViewController.m // 03-应用管理 // // Creat ...
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
随机推荐
- think in uml 1
对象,在过程的基础上,是一个抽象级别的提升,可以构建更大更复杂的系统 数据流图(Data Flow Diagram):简称DFD,它从数据传递和加工角度,以图形方式来表达系统的逻辑功能.数据在系统内部 ...
- 更改 android realtek的系统权限
由于在 在删除系统的文件时候出现 Read-only file system,所以要获取权限. 推出shell adb mount mount -o rw,remount /system 就可以了
- web-service客户端与服务器端的连接
1 首先讲解下xfire XFire是新一代的Java Web服务引擎,XFire使得在JavaEE应用中发布Web服务变得轻而易举.和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地 ...
- Linux下安装php开发框架yaf
yaf框架中文手册:http://yaf.laruence.com/manual/index.html yaf手册:http://www.php.net/manual/en/book.yaf.php ...
- spring多个数据源配置
sys.properties中的内容 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver DB.url=jdbc\:oracle\:thin\:@ ...
- MVVM 入门介绍
转载自:http://www.objccn.io/issue-13-1/ 我于 2011 年在 500px 找到自己的第一份 iOS 开发工作.虽然我已经在大学里做了好几年 iOS 外包开发,但这才是 ...
- python解析XML之ElementTree
#coding=utf-8 from xml.etree import ElementTree as ET tree=ET.parse('test.xml') root = tree.getroot( ...
- AI 人工智能 探索 (七)
我简单画了一幅图,来讲下 ai 中的设计模式.图形中的这些人物,我想大家都能看的明白. 当 盗贼出现,人们发现了他们,就 呼叫 主类,然后主类再 通知 下面对应的管理局,然后管理局再 分配人手过去 ...
- 使用PreListener与InteractionListener的一个小发现
如果两个刚体使用PreListener忽略了它的碰撞时,如果这时你使用InteractionListener来侦听它们的碰撞还是可以侦听到的: package { import com.bit101. ...
- Class.forName() 初始化、Thread.currentThread().getContextClassLoader().getResourceAsStream
Class.forName() 和 ClassLoader.loadClass()的区别? Class.forName() 和 Class.forName().NewInstance()的区别? Cl ...