iOS开发UI篇—ios应用数据存储方式(归档)

简介:

一、简单说明

在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦;

偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息)

归档:因为前两者都有一个致命的缺陷,只能存储常用的类型。归档可以实现把自定义的对象存放在文件中。

二、代码示例

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保存的数据是直接显示的,不安全。通过归档方法保存的数据在文件中打开是乱码的,更安全。

目录
相关文章
|
16天前
|
前端开发 编解码 数据格式
浅谈响应式编程在企业级前端应用 UI 开发中的实践
浅谈响应式编程在企业级前端应用 UI 开发中的实践
17 0
浅谈响应式编程在企业级前端应用 UI 开发中的实践
|
1月前
|
Web App开发 数据采集 前端开发
纯技术讨论:如何让 SAP UI5 应用无法被别人在浏览器里调试 - 这种做法不推荐试读版
纯技术讨论:如何让 SAP UI5 应用无法被别人在浏览器里调试 - 这种做法不推荐试读版
15 0
|
1月前
|
XML 存储 数据格式
SAP UI5 控件 customData 属性的应用介绍
SAP UI5 控件 customData 属性的应用介绍
31 0
|
2月前
|
iOS开发 开发者
苹果iOS App Store上架操作流程详解:从开发者账号到应用发布
很多开发者在开发完iOS APP、进行内测后,下一步就面临上架App Store,不过也有很多同学对APP上架App Store的流程不太了解,下面我们来说一下iOS APP上架App Store的具体流程,如有未涉及到的部分,大家可以及时咨询,共同探讨。
|
2月前
|
开发者 iOS开发
iOS应用上架详细图文教程(上)
App Store作为苹果官方的应用商店,审核严格周期长一直让用户头疼不已,很多app都“死”在了审核这一关,那我们就要放弃iOS用户了吗?当然不是!本期我们从iOS app上架流程开始梳理,详细了解下iOS app上架的那些事。
|
2月前
|
Swift iOS开发 开发者
iOS 应用上架流程详解
iOS 应用上架流程详解
|
2月前
|
Android开发 iOS开发 UED
appuploader   iOS 应用自动发布
appuploader   iOS 应用自动发布
|
12天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
27天前
|
安全 数据安全/隐私保护 虚拟化
iOS应用加固方案解析:ipa加固安全技术全面评测
iOS应用加固方案解析:ipa加固安全技术全面评测
36 3
|
1月前
|
运维 监控 安全
应用研发平台EMAS常见问题之sophix ios flutter热更新如何解决
应用研发平台EMAS(Enterprise Mobile Application Service)是阿里云提供的一个全栈移动应用开发平台,集成了应用开发、测试、部署、监控和运营服务;本合集旨在总结EMAS产品在应用开发和运维过程中的常见问题及解决方案,助力开发者和企业高效解决技术难题,加速移动应用的上线和稳定运行。
77 0

热门文章

最新文章