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实例,通过 ...
随机推荐
- ThinkPHP3.2 整合支付宝RSA加密方式
RSA核心加密验证算法 <?php /** * RSA签名 * @param $data 待签名数据 * @param $private_key 商户私钥字符串 * return 签名结果 */ ...
- 通过js控制层的动态隐藏
<style type="text/css"> #dv1{ width:1000px; height:1000px; overflow:hidden; display: ...
- Vue2.0以后,有哪些变化
最近移动端项目版本升级,Vue由之前的1.0升级到2.3,那么,Vue2.0之后,有哪些细节的变化呢,现在总结如下: 1.在每个组件模板,不再支持片段代码 组件中模板: 之前: <templat ...
- oracle相关常识
1.数据类型 VARCHAR2() NUMBER() DATE CLOB BLOB 2.复制表:create table tableName as select * from emp3.新增列:ALT ...
- 我为什么不用Django而用Flask?
前言 对于初学者来说,找到一个好的框架来学习或者项目开发都是非常有必要的,而当你有一定开发经验后,你应该选择适合当前业务需要的框架.我这里并不想探讨哪个框架好哪个不好,这个永恒的话题就跟探讨“世界上哪 ...
- C语言腾讯课堂(一)
腾讯课堂:c语言从零到精通 1. 从第一个例子开始 #include <stdio.h> int main(void) { printf("hello, qin men \n&q ...
- linux 中环境变量配置文件说明
1. 修改/etc/profile文件 特点:所有用户的shell都有权使用你配置好的环境变量 说明:如果你的电脑仅用作开发,建议使用此配置,因为所有用户的shell都有权使用你配置好的环境变量,所以 ...
- VBA注意事项
以下是项目过程中遇到的坑,可能有些说明的部分不一定严谨,仅供参考 1.最好保存成 [*.xlsm]文件 2.注意 VBA 的参数类型,使用的参数如果未声明直接使用的话会出现类型不匹配的错误 3.代码写 ...
- 【Leetcode】【Easy】Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- WAKE-WIN10-SOFT-GITHUB
1,GITHUB 官网:https://github.com/ 2,软件工具 ,,,,,,