一.简单说明 
  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. 5、Angular2 Injectable 服务

    1.Injectable 

  2. TFS2013 设置签出独占锁(转载)

    作者:晓菜鸟 出处:http://www.cnblogs.com/52XF/p/4239056.html 在使用TFS进行源代码管理的时候VS默认允许多个签出,但在团队开发中往往需要设置独占锁(排他锁 ...

  3. C# ADO.NET 面向对象

    ADO.NET跟面向对象的结合 把面向对象跟数据库连接用 在项目里面创建一个新的文件夹   名字为App_Code 在这个App_Code里面创建几个类 主要为拆分问题,标上序号,先干什么在干什么 实 ...

  4. Java 访问权限控制- protected 关键字

    protected 关键字的真正内涵 文章来源:http://blog.csdn.net/justloveyou_/article/details/61672133 很多介绍Java语言的书籍(包括& ...

  5. webpack管理资源

    加载Css webpack并不能处理js以外的静态资源,通过loader来支持他们 npm install --save-dev style-loader css-loader const path ...

  6. js中各个排序算法和sort函数的比较

    js中要实现数据排序,其实只需要用sort函数就能很好的满足了,但是我今天想知道他和其他排序算法的区别,比如耗时呀等.测了一组数据如下: // ---------- 一些排序算法 Sort = {} ...

  7. 常用DOM结构方法总结

    ---内容开始--- 获取元素的方法: getElementById() 通过ID名获取元素 getElementsByTagName() 通过元素(标签)名称 getElementsByClassN ...

  8. xcode选择开发者时"The Apple Developer Program License Agreement has been updated"

    解决方法:进入开发者中心查看红色提示信息,同意就行

  9. SpringBoot 启动参数设置环境变量、JVM参数、tomcat远程调试

    java命令的模版:java [-options] -jar jarfile [args...] 先贴一下我的简单的启动命令: java -Xms128m -Xmx256m -Xdebug -Xrun ...

  10. Windows远程桌面,出现身份验证错误,要求的函数不正确

    升级windows10 1803后,mstsc远程桌面出现 mstsc 远程桌面要求的函数不受支持,这可能是由于 CredSSP 加密 Oracle 修正.如图所示: 运行(win+r) gpedit ...