ios应用数据存储方式(归档) - 转
一.简单说明
1.在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦。
2.偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息)。
3.归档:因为前两者都有一个致命的缺陷,只能存储常用的类型。归档可以实现把自定义的对象存储放在文件中。
1.代码示例 DBPerson.h文件
//如果想将一个自定义对象保存到文件中必须实现NSCoding协议
@interface DBPerson : NSObject <NSCoding>
//姓名
@property(nonatomic,copy) NSString *name;
//年龄
@property(nonatomic,assign) int age;
//身高
@property(nonatomic,assign) double height;
@end
DBPerson.m文件
#import "DBPerson.h"
@implementation DBPerson
/**
* 1.当将一个自定义对象保存到文件的时候就会调用该方法
* 2.在该方法中说明如何存储自定义对象的属性
* 3.也就说在该方法中说清楚存储自定义对象的哪些属性
*/
- (void)encodeWithCoder:(NSCoder *)aCoder{
NSLog(@"调用了encodeWithCoder:方法");
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeDouble:self.height forKey:@"height"];
} /**
* 1.当从文件中读取一个对象的时候就会调用该方法
* 2.在该方法中说明如何读取保存在文件中的对象
* 3.也就是说在该方法中说清楚怎么读取文件中的对象
*/
- (id)initWithCoder:(NSCoder *)aDecoder{
NSLog(@"调用了initWithCoder:方法");
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = (int)[aDecoder decodeIntegerForKey:@"age"];
self.height = [aDecoder decodeDoubleForKey:@"height"];
}
return self;
}
@end
ViewController.m文件
#import "ViewController.h"
#import "DBPerson.h" #define CURRENT_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define CURRENT_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height - 64)
#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 40 @interface ViewController ()
//保存数据按钮
@property(nonatomic,strong) UIButton *saveButton;
//读取数据按钮
@property(nonatomic,strong) UIButton *readButton;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self initControl];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} //初始化控件
- (void)initControl{
_saveButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/ - BUTTON_WIDTH/,
CURRENT_SCREEN_HEIGHT/ - BUTTON_HEIGHT,
BUTTON_WIDTH,
BUTTON_HEIGHT)];
[_saveButton setTitle:@"保存数据" forState:UIControlStateNormal];
[_saveButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_saveButton addTarget:self
action:@selector(saveClick)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_saveButton]; _readButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/ - BUTTON_WIDTH/,
_saveButton.frame.origin.y + _saveButton.frame.size.height + ,
BUTTON_WIDTH,
BUTTON_HEIGHT)];
[_readButton setTitle:@"读取数据" forState:UIControlStateNormal];
[_readButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_readButton addTarget:self
action:@selector(readClick)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_readButton]; } #pragma mark -按钮事件
- (void)saveClick{
//创建对象
DBPerson *person = [[DBPerson alloc] init];
person.name = @"文丁丁";
person.age = ;
person.height = 1.76f; //获取文件路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path = %@",path); //将自定义的对象保存到文件中
[NSKeyedArchiver archiveRootObject:person
toFile:path];
} - (void)readClick{
//获取文件路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"person.yangyang"]; //从文件中读取对象
DBPerson *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"name = %@",person.name);
NSLog(@"age = %d",person.age);
NSLog(@"height = %f",person.height);
}
@end
三.继承类中的使用
DBStudent.h文件
#import "DBPerson.h"
@interface DBStudent : DBPerson
//增加一个体重属性
@property(nonatomic,assign) double weight;
@end
DBStudent.m文件
#import "DBStudent.h"
@implementation DBStudent
//在子类中重写这两个方法
- (void)encodeWithCoder:(NSCoder *)aCoder{
[super encodeWithCoder:aCoder];
NSLog(@"调用了DBStudent encodeWithCoder");
[aCoder encodeFloat:self.weight forKey:@"weight"];
} - (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"调用了DBStudent initWithCoder");
self.weight = [aDecoder decodeFloatForKey:@"weight"];
}
return self;
}
@end
ViewController.m文件
#import "ViewController.h"
#import "DBStudent.h" #define CURRENT_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define CURRENT_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height - 64)
#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 40 @interface ViewController ()
//保存数据按钮
@property(nonatomic,strong) UIButton *saveButton;
//读取数据按钮
@property(nonatomic,strong) UIButton *readButton;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self initControl];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} //初始化控件
- (void)initControl{
_saveButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/ - BUTTON_WIDTH/,
CURRENT_SCREEN_HEIGHT/ - BUTTON_HEIGHT,
BUTTON_WIDTH,
BUTTON_HEIGHT)];
[_saveButton setTitle:@"保存数据" forState:UIControlStateNormal];
[_saveButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_saveButton addTarget:self
action:@selector(saveClick)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_saveButton]; _readButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/ - BUTTON_WIDTH/,
_saveButton.frame.origin.y + _saveButton.frame.size.height + ,
BUTTON_WIDTH,
BUTTON_HEIGHT)];
[_readButton setTitle:@"读取数据" forState:UIControlStateNormal];
[_readButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_readButton addTarget:self
action:@selector(readClick)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_readButton]; } #pragma mark -按钮事件
- (void)saveClick{
// //创建对象
// DBPerson *person = [[DBPerson alloc] init];
// person.name = @"文丁丁";
// person.age = 31;
// person.height = 1.76f; DBStudent *student = [[DBStudent alloc] init];
student.name = @"wendingding";
student.age = ;
student.height = 1.80f;
student.weight = ; //获取文件路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path = %@",path); //将自定义的对象保存到文件中
[NSKeyedArchiver archiveRootObject:student
toFile:path];
} - (void)readClick{
//获取文件路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"person.yangyang"]; //从文件中读取对象
DBStudent *student = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"name = %@",student.name);
NSLog(@"age = %d",student.age);
NSLog(@"height = %f",student.height);
NSLog(@"weight = %f",student.weight);
}
@end
四.重要说明
1.保存数据过程
// //创建对象
// DBPerson *person = [[DBPerson alloc] init];
// person.name = @"文丁丁";
// person.age = 31;
// person.height = 1.76f; DBStudent *student = [[DBStudent alloc] init];
student.name = @"wendingding";
student.age = ;
student.height = 1.80f;
student.weight = ; //获取文件路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path = %@",path); //将自定义的对象保存到文件中
[NSKeyedArchiver archiveRootObject:student
toFile:path];
2.读取数据过程
//获取文件路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"person.yangyang"]; //从文件中读取对象
DBStudent *student = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"name = %@",student.name);
NSLog(@"age = %d",student.age);
NSLog(@"height = %f",student.height);
NSLog(@"weight = %f",student.weight);
3.遵守 NSCoding 协议,并实现该协议中的两个方法。
4.如果是继承,则子类一定要重写那两个方法。因为person的子类在存取的时候,会去子类中去找调用的方法,没找到那么它就去父类中找,所以最好保存和读取的时候新增加的属性会被忽略。需要先调用父类的方法,先初始化父类的,在初始化子类的。
5.保存数据的文件的后缀名可以随意命名。
6.通过plist保存的数据是直接显示的,不安全。通过归档方法的数据在文件中打开是乱码的,更安全。
ios应用数据存储方式(归档) - 转的更多相关文章
- iOS开发UI篇—ios应用数据存储方式(归档)
iOS开发UI篇—ios应用数据存储方式(归档) 一.简单说明 在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦: 偏好设置(将所有的东西都保存在同 ...
- iOS开发UI篇—ios应用数据存储方式(归档) :转发
本文转发至:文顶顶http://www.cnblogs.com/wendingding/p/3775293.html iOS开发UI篇—ios应用数据存储方式(归档) 一.简单说明 在使用plist ...
- iOS 应用数据存储方式(XML属性列表-plist)
iOS 应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) ...
- 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应用数据存储方式
一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) 4.SQLite3(数据库,关系型数据库,不能直接存储对 ...
- ios应用数据存储方式(XML属性列表-plist) - 转
一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) 4.SQLite3(数据库,关系型数据库,不能直 ...
- iOS应用 数据存储方式 (一)
沙盒是每个应用程序的空间,每个应用程序只能访问自己的文件夹,不可以跨越,访问别的程序的文件夹,这个文件夹就是该应用程序的沙盒. 沙盒中包括以下几个文件夹: 1.应用程序包:(Layer)包含了所有资源 ...
- ios应用数据存储方式(偏好设置)-转
一.简单介绍 1.很多ios应用都支持偏好设置,比如保存用户名,密码,字体大小等设置,ios提供了一套标准的解决方案来为应用加入偏好设置功能. 2.每个应用都有个NSUserDefaults实例,通过 ...
随机推荐
- c# 快速修改图片颜色
public static void ChangeColour(this Bitmap bmp, byte inColourR, byte inColourG, byte inColourB, byt ...
- Hashtable(哈希表)
简体字繁体字转化: class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ; i < ...
- 深入理解java线程池—ThreadPoolExecutor
几句闲扯:首先,我想说java的线程池真的是很绕,以前一直都感觉新建几个线程一直不退出到底是怎么实现的,也就有了后来学习ThreadPoolExecutor源码.学习源码的过程中,最恶心的其实就是几种 ...
- java消息中间件 RocketMQ Linux安装与运行
阿里巴巴宣布捐赠RocketMQ到Apache软件基金会孵化项目,最近闲下来便去部署了一个试验版本玩玩. 至于RockeMQ是什么,原理架构什么的这里就不赘述了,这里只记录安装过程. 一.系统环境 s ...
- lxml模块(应用xpath技术)
一.lxml介绍 第三方库lxml是第一款表现出高性能特征的python xml库,天生支持Xpath1.0.XSLT1.0.定制元素类,甚至python风格的数据绑定接口.lxml是通过Cpytho ...
- webpack、babel模块、模块化
一.webpack介绍 webpack这个工具非常强大,解决了前端很繁琐的一些工具流程繁琐的事情.中文官网链接地址:https://www.webpackjs.com/ 1.为什么要使用webpack ...
- phpmyadmin杂记
看着坑爹的教程..我老是报错我也很无奈啊 以下几项必改 $cfg['blowfish_secret'] = ' ';//这里引号内是空格,实际上可以是任意内容, $cfg['Servers'][$i] ...
- SQL Server中的高可用性----复制
在本系列文章的前两篇对高可用性的意义和单实例下的高可用性做了阐述.但是当随着数据量的增长,以及对RTO和RPO要求的严格,单实例已经无法满足HA/DR方面的要求,因此需要做多实例的高可用性.本文着重对 ...
- spring配置连接池和dao使用jdbcTemplate
1 spring配置c3p0连接池 第一步 导入jar包 第二步 创建spring配置文件,配置连接池 (1)把代码中的实现在配置文件中实现 2 dao使用jdbcTemplate (1) 创建ser ...
- File not Found:DockForm.dcu的解决办法
安装控件时,如果引用了dsgnintf单元,那么就会提示找不到proxy.pas 或者DockForm.dcu的错误,只需在安装控件包时添加“lib\DesignIde.dcp”即可