一、简介

  在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦;

  偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息

  归档:因为前两者都有一个致命的缺陷,只能存储常用的类型。归档可以实现把自定义的对象存放在文件中。

二、使用

#import "ViewController.h"
#import "Person.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad];
//1.创建对象
Person *p=[[Person alloc]init];
p.name=@"deng";
p.age=;
p.height=1.7; //2.获取文件路径
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.deng"];
NSLog(@"path=%@",path); //3.将自定义的对象保存到文件中
[NSKeyedArchiver archiveRootObject:p toFile:path];
} -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{ //1.获取文件路径
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.deng"];
NSLog(@"path=%@",path);
//2.从文件中读取对象
Person *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
}
@end

person.h文件

#import <Foundation/Foundation.h>

@interface Person : NSObject <NSCoding>

@property(nonatomic,strong)NSString *name;
@property(nonatomic,assign)int age;
@property(nonatomic,assign)double height; @end

person.m

#import "Person.h"

@implementation Person

-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInt:self.age forKey:@"age"];
[aCoder encodeDouble:self.height forKey:@"height"];
} -(id)initWithCoder:(NSCoder *)aDecoder
{
if (self == [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
self.height = [aDecoder decodeDoubleForKey:@"height"];
}
return self;
} @end

三、注意

1.一定要实现协议<NSCoding>,并在要保存对象的.m文件中实现两个协议方法(如果不实现会报错):

  -(void)encodeWithCoder:(NSCoder *)aCoder

   -(id)initWithCoder:(NSCoder *)aDecoder

2.保存保存时对象的属性类型一定要注意,要用对应的方法保存对应的属性。

3.读取出来的数据一定要赋给对象的属性。

iOS数据持久化--归档的更多相关文章

  1. iOS开发笔记-swift实现iOS数据持久化之归档NSKeyedArchiver

    IOS数据持久化的方式分为三种: 属性列表 (plist.NSUserDefaults) 归档 (NSKeyedArchiver) 数据库 (SQLite.Core Data.第三方类库等 归档(又名 ...

  2. IOS数据持久化之归档NSKeyedArchiver

    IOS数据持久化的方式分为三种: 属性列表 (自定义的Property List .NSUserDefaults) 归档 (NSKeyedArchiver) 数据库 (SQLite.Core Data ...

  3. iOS 数据持久化(扩展知识:模糊背景效果和密码保护功能)

    本篇随笔除了介绍 iOS 数据持久化知识之外,还贯穿了以下内容: (1)自定义 TableView,结合 block 从 ViewController 中分离出 View,轻 ViewControll ...

  4. iOS -数据持久化方式-以真实项目讲解

    前面已经讲解了SQLite,FMDB以及CoreData的基本操作和代码讲解(CoreData也在不断学习中,上篇博客也会不断更新中).本篇我们将讲述在实际开发中,所使用的iOS数据持久化的方式以及怎 ...

  5. iOS数据持久化方式及class_copyIvarList与class_copyPropertyList的区别

    iOS数据持久化方式:plist文件(属性列表)preference(偏好设置)NSKeyedArchiver(归档)SQLite3CoreData沙盒:iOS程序默认情况下只能访问自己的程序目录,这 ...

  6. iOS数据持久化存储:归档

    在平时的iOS开发中,我们经常用到的数据持久化存储方式大概主要有:NSUserDefaults(plist),文件,数据库,归档..前三种比较经常用到,第四种归档我个人感觉用的还是比较少的,恰恰因为用 ...

  7. iOS 数据持久化(1):属性列表与对象归档

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  8. IOS数据持久化之归档NSKeyedArchiver, NSUserDefaults,writeToFile

    //2.文件读写 //支持:NSString, NSArray , NSDictionay, NSData //注:集合(NSArray, NSDictionay)中得元素也必须是这四种类型, 才能够 ...

  9. iOS数据持久化-OC

    沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...

随机推荐

  1. 微信小程序请求数据

    微信小程序请求数据,在页面展示,可以在onLoad生命周期中进行请求. 1.新建目录http,新建文件http.js 2.在js文件中暴露需要使用的变量 var baseUrl = 'http://1 ...

  2. IntelliJ IDEA 2017版 spring-boot 2.03后 Pageable用法;Pageable用法,PageRequest过时,新用法;Pageable过时问题;

    1.旧版本Pageable用法: 但是会显示,这个版本已经过时,这时可以查看源码. 一般,一个方法过时,就会在其附近形成一个新的同名的但是其他用法的方法.按照这个理念,来找这个源码.很幸运,蒙对了,我 ...

  3. new命令简化的内部流程

    构造函数返回对象的一些问题: function fn(name,age){ this.name = name; this.age = age; //return 23; 忽略数字,直接返回原有对象 / ...

  4. matlab生成滤波器系数组

    用MATLAB生成的滤波器系数是可以控制增益的,一般归一化的目的是控制增益为1.滤波器的阶数由数据速率,过渡带宽.通带波纹和阴带波纹来决定, 在下图中FS,Apass,Astop固定之后,只要Fpas ...

  5. poj 1094 Sorting It All Out 拓补排序

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  6. poj 1068 Parencodings 模拟题

    Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...

  7. Spring MVC 指导文档解读(二)

    Special Bean Types In the WebApplicationContext 解读 1.WebApplicationContext 特有的几种 Bean Types 2. 也表明 与 ...

  8. 用css写出下拉框(代码转自wq群)

    做网易云音乐首页时遇到的问题,鼠标指在右上角头像时出现下拉框. <style>/* css*/ #body{ float: left; } #xialakuang{ background- ...

  9. Linux 禁ping和开启ping操作

    方法一:     # echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all 如果要恢复,只要: # echo 0 > /proc/sys/net ...

  10. [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)

    http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...