一.简单说明 
  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应用数据存储方式(归档) - 转的更多相关文章

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

    iOS开发UI篇—ios应用数据存储方式(归档)  一.简单说明 在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦: 偏好设置(将所有的东西都保存在同 ...

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

    本文转发至:文顶顶http://www.cnblogs.com/wendingding/p/3775293.html iOS开发UI篇—ios应用数据存储方式(归档)  一.简单说明 在使用plist ...

  3. iOS 应用数据存储方式(XML属性列表-plist)

    iOS 应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) ...

  4. iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)

    iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存 ...

  5. iOS开发UI篇—ios应用数据存储方式(偏好设置)

    iOS开发UI篇—ios应用数据存储方式(偏好设置) 一.简单介绍 很多iOS应用都支持偏好设置,比如保存用户名.密码.字体大小等设置,iOS提供了一套标准的解决方案来为应用加入偏好设置功能 每个应用 ...

  6. ios应用数据存储方式

    一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) 4.SQLite3(数据库,关系型数据库,不能直接存储对 ...

  7. ios应用数据存储方式(XML属性列表-plist) - 转

    一.ios应用常用的数据存储方式  1.plist(XML属性列表归档)  2.偏好设置  3.NSKeydeArchiver归档(存储自定义对象)  4.SQLite3(数据库,关系型数据库,不能直 ...

  8. iOS应用 数据存储方式 (一)

    沙盒是每个应用程序的空间,每个应用程序只能访问自己的文件夹,不可以跨越,访问别的程序的文件夹,这个文件夹就是该应用程序的沙盒. 沙盒中包括以下几个文件夹: 1.应用程序包:(Layer)包含了所有资源 ...

  9. ios应用数据存储方式(偏好设置)-转

    一.简单介绍 1.很多ios应用都支持偏好设置,比如保存用户名,密码,字体大小等设置,ios提供了一套标准的解决方案来为应用加入偏好设置功能. 2.每个应用都有个NSUserDefaults实例,通过 ...

随机推荐

  1. asp.net FileUpload上传文件夹并检测所有子文件

    1.在FileUpload控件添加一个属性 webkitdirectory=""就可以上传文件夹了 <asp:FileUpload ID="FileUpload1& ...

  2. DevExpress 14.2 批量汉化

    1.下载DevExpress_.NET_Localization_Resources_14.2汉化包 2.解压后将zh-CN或zh-CHS复制到安装目录如D:\Program Files (x86)\ ...

  3. 认识Spring AOP

    Spring AOP AOP是Aspect/'æspekt/ Oriented/ɔːrɪentɪd/ Programming的缩写,意为:面向切面编程. 是什么 通过预编译方式和运行期动态代理实现程序 ...

  4. STL库中string类内存布局的探究

    在STL中有着一个类就是string类,他的内存布局和存储机制究竟是怎么样的呢? 这就是建立好的string 可以看出,图中用黄色框框标注的部分就是主要区域 我们用来给string对象进行初始化的字符 ...

  5. IO流之Properties类

    Properties类介绍 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串. 特点: 1.Hashtable的 ...

  6. css3制作有动画效果的面板

    .show-panel .slide-panels{ right: 0px; } .slide-panels{ z-index: 101; background: #fff; position: fi ...

  7. SharePoint Tricks - HTML & CSS & JavaScript

    1. 隐藏Quick Launch <style type="text/css">/*-- Hide Quick Launch --*/#s4-leftpanel{   ...

  8. HTML 5入门知识(二)

    使用HTML 5结构标签 <article> <article>标签可以在网页中定义独立的内容,包括文章.博客和用户评论等.一个article元素通常有它自己的标题,一般放在一 ...

  9. java面试题全集(上)--java基础

    本文转载自:https://blog.csdn.net/jackfrued/article/details/44921941 1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - ...

  10. 通过 Powershell 来调整 ARM 模式下虚拟机的尺寸

    需求描述 在部署完 ARM 模式的虚拟机以后,可以通过 PowerShell 命令来调整虚拟机的尺寸,以下是通过 PowerShell 命令来调整 ARM 模式的虚拟机尺寸. Note 本文只限于 A ...