iOS开发UI篇—ios应用数据存储方式(归档)
iOS开发UI篇—ios应用数据存储方式(归档)
一、简单说明
二、代码示例
1.文件结构

2.代码示例
YYViewController.m文件
//
// YYViewController.m
// 02-归档
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYViewController.h"
#import "YYPerson.h" @interface YYViewController ()
- (IBAction)saveBtnOnclick:(id)sender;
- (IBAction)readBtnOnclick:(id)sender; @end @implementation YYViewController - (void)viewDidLoad
{
[super viewDidLoad];
} - (IBAction)saveBtnOnclick:(id)sender {
//1.创建对象
YYPerson *p=[[YYPerson alloc]init];
p.name=@"文顶顶";
p.age=;
p.height=1.7; //2.获取文件路径
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path); //3.将自定义的对象保存到文件中
[NSKeyedArchiver archiveRootObject:p toFile:path]; } - (IBAction)readBtnOnclick:(id)sender {
//1.获取文件路径
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path); //2.从文件中读取对象
YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
}
@end
新建一个person类
YYPerson.h文件
//
// YYPerson.h
// 02-归档
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import <Foundation/Foundation.h> // 如果想将一个自定义对象保存到文件中必须实现NSCoding协议
@interface YYPerson : NSObject<NSCoding> //姓名
@property(nonatomic,copy)NSString *name;
//年龄
@property(nonatomic,assign)int age;
//身高
@property(nonatomic,assign)double height;
@end
YYPerson.m文件
//
// YYPerson.m
// 02-归档
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYPerson.h" @implementation YYPerson // 当将一个自定义对象保存到文件的时候就会调用该方法
// 在该方法中说明如何存储自定义对象的属性
// 也就说在该方法中说清楚存储自定义对象的哪些属性
-(void)encodeWithCoder:(NSCoder *)aCoder
{
NSLog(@"调用了encodeWithCoder:方法");
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeDouble:self.height forKey:@"height"];
} // 当从文件中读取一个对象的时候就会调用该方法
// 在该方法中说明如何读取保存在文件中的对象
// 也就是说在该方法中说清楚怎么读取文件中的对象
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"调用了initWithCoder:方法");
//注意:在构造方法中需要先初始化父类的方法
if (self=[super init]) {
self.name=[aDecoder decodeObjectForKey:@"name"];
self.age=[aDecoder decodeIntegerForKey:@"age"];
self.height=[aDecoder decodeDoubleForKey:@"height"];
}
return self;
}
@end
3.打印效果和两个重要的错误提示
点击保存按钮和读取按钮,成功打印结果如下:

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

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

三、继承类中的使用
新建一个学生类,让这个类继承自Preson这个类,增加一个体重的属性。
YYstudent.h文件
//
// YYstudent.h
// 02-归档
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYPerson.h" @interface YYstudent : YYPerson
//增加一个体重属性
@property(nonatomic,assign) double weight;
@end
YYstudent.m文件
//
// YYstudent.m
// 02-归档
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYstudent.h" @implementation YYstudent //在子类中重写这两个方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder];
NSLog(@"调用了YYStudent encodeWithCoder");
[aCoder encodeFloat:self.weight forKey:@"weight"];
} - (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"调用了YYstudent initWithCoder");
self.weight = [aDecoder decodeFloatForKey:@"weight"];
}
return self;
}
@end
YYViewController.m文件
//
// YYViewController.m
// 02-归档
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYViewController.h"
#import "YYPerson.h"
#import "YYstudent.h" @interface YYViewController ()
- (IBAction)saveBtnOnclick:(id)sender;
- (IBAction)readBtnOnclick:(id)sender; @end @implementation YYViewController - (void)viewDidLoad
{
[super viewDidLoad];
} - (IBAction)saveBtnOnclick:(id)sender {
//1.创建对象
// YYPerson *p=[[YYPerson alloc]init];
// p.name=@"文顶顶";
// p.age=23;
// p.height=1.7; YYstudent *s=[[YYstudent alloc]init];
s.name=@"wendingding";
s.age=;
s.height=1.7;
s.weight=;
//2.获取文件路径
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path); //3.将自定义的对象保存到文件中
// [NSKeyedArchiver archiveRootObject:p toFile:path];
[NSKeyedArchiver archiveRootObject:s toFile:path]; } - (IBAction)readBtnOnclick:(id)sender {
//1.获取文件路径
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path); //2.从文件中读取对象
// YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
// NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
YYstudent *s=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@,%d,%.1f,%f",s.name,s.age,s.height,s.weight);
}
@end
点击保存按钮和读取按钮后的打印输出:

四、重要说明
1.保存数据过程:
//1.创建对象
YYstudent *s=[[YYstudent alloc]init];
s.name=@"wendingding";
s.age=;
s.height=1.7;
s.weight=; //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应用数据存储方式(归档) :转发
本文转发至:文顶顶http://www.cnblogs.com/wendingding/p/3775293.html 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 ...
随机推荐
- Leetcode: Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- SQL Server数据库性能优化(二)之 索引优化
参考文献 http://isky000.com/database/mysql-performance-tuning-index 原文作者是做mysql 优化的 但是我觉得 在索引方面 ...
- Intel+Ardruino 101 翻转时点灯
/* =============================================== Example sketch for CurieIMU library for Intel(R ...
- wex5 教程之 图文讲解 Cloudx5一键部署
视频教程地址:http://v.youku.com/v_show/id_XMTc3OTExNTUwNA==.html 效果预览: 一键部署cloudx5三要领 1.数据源命名为x5 2.数据库命名为x ...
- odd_even_list
public class Solution { public ListNode OddEvenList(ListNode head) { if(head == null || head.next == ...
- 隐式意图Intent
在我们想往下一个页面传递数据时,要想到显式意图和隐式意图,显示意图用于内部活动跳转时比较方便,而隐式意图用于应用程序中外部活动的跳转时较为方便,在使用隐式意图时我们要想到清单文件 代码如下: < ...
- 微软 WP 新策略:非开发者账号免费解锁 1 部手机(转)
好消息,微软现在非开发者用户解锁1部WP8手机和部署两个应用,即我们只 需注册个windows live账号即可解锁我们的WP8了!! 准备工作:1.需要一台装有windows8 64位系统的电脑. ...
- Hibernate不能自动建数据表解决办法
首先自己要注意自己的MYSQL版本,然后设置对应的方言 兼容性模式 <property name="hibernate.dialect">org.hibernate.d ...
- MSDN for VS2012 的安装
在VS2012中,由于MSDN默认不内置,VS2008 以上的就没有独立的 MSDN 了 ,而是被 Microsoft Help Viewer 取代了. 该组件包含在 VS2012 的 ISO 安装镜 ...
- JMeter结果分析