数据持久化就是数据保存成文件,存储到程序中的沙盒中.

沙盒构成

Document 存储用户数据,需要备份的信息

Caches 缓存文件,
程序专用的支持文件

Temp 临时文件

通过代码查找程序沙盒的相对路径

NSArray

*documentsPathArry NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

NSUserDomainMask,

YES);

NSString

*document = [documentsPathArry
lastObject];

  
NSLog(@"%@",
document);
        //  
缓存文件夹路径

NSArray
*cachesPatharray =
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,

NSUserDomainMask,

YES);

   
NSString
*cachespath = cachesPatharray[0];

   
NSLog(@"%@",
cachespath);

   

//
打印temp文件夹

//
该文件夹一般存储
临时文件夹

   
NSString
*tempPath =
NSTemporaryDirectory();

   
NSLog(@"%@",
tempPath);

         //

打印沙盒主目录路径  NSHomeDirectory()

NSString
*homePath =
NSHomeDirectory();

NSLog(@"%@",
homePath);

//

简单对象写入文件

//

注意
:如果你写入字典或者数组

那么数组字典中存储的数据必须是简单对象
无法写入复杂对象

- (void)writeFile

{

   
//
简单对象

   
//
字符串
字典
数组
data...系统写好的类

   
//
写入文件的路径

   
//
写入documents

路径下写入xiaoshuo.text

   
NSArray
*documentsArray =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

NSUserDomainMask,

YES);

   
NSString
*document = documentsArray[0];

   
NSString
*path = [document
stringByAppendingString:@"/xiaoshuo.text"];

   
NSString
*str =
@"第一章

在一个月黑风高的早上";

   

   
// atomically
如果yes

在你写入的过程中出现程序崩溃
不影响写入

    [str
writeToFile:path

atomically:YES

encoding:NSUTF8StringEncoding

error:nil];

   
NSLog(@"%@",
path);

   

   
//
简单拼接对象写入步骤

   
// 1.拼接要写入的路径

(注意的路径一定要拼接对)

   
// 2.
调用写方法完事

   

   
//
写入一个数组
shuzu.plist

   
//
必须给后缀类型
你不给呢
就默认是text格式

   
NSString
*arrPath = [document
stringByAppendingPathComponent:@"shuzu.plist"];

   
NSArray
*array =
@[@"永乐",

@"永飞",

@"哈哈"];

   
//
调用写入方法

    [array
writeToFile:arrPath

atomically:YES];

   
NSLog(@"%@",
arrPath);

   

   
//
写入一个字典
zidian.plist

   
NSString
*dicPath = [document
stringByAppendingPathComponent:@"zidian.plist"];

   
NSDictionary
*dic =
@{@"name":

@"xiaofang"};

    [dic
writeToFile:dicPath

atomically:YES];

   
NSLog(@"%@",
dicPath);

   

   
// data的写入

后缀.da

   
NSString
*dataPath = [document
stringByAppendingPathComponent:@"data.da"];

   
NSString
*dataStr =
@"你猜我是谁";

   
NSData
*data = [dataStr
dataUsingEncoding:NSUTF8StringEncoding];

   
//
写入文件

    [data
writeToFile:dataPath

atomically:YES];

   
NSLog(@"%@",
dataPath);

   

   
//
复杂对象

   
//
自定义的类
比如person

}

//

读取写入的文件

- (void)readingFile

{

   
//
读字符串

   
//
获取路径

   
NSArray
*documentsArray =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

NSUserDomainMask,

YES);

   
NSString
*document = documentsArray[0];

   
NSString
*path = [document
stringByAppendingString:@"/xiaoshuo.text"];

   

   
//
从路径中读取字符串

   
NSString
*str = [[NSString

alloc]

initWithContentsOfFile:path

encoding:NSUTF8StringEncoding

error:nil];

   
NSLog(@"%@",
str);

   

   
//
读取数组的文件

   
NSString
*arrPath = [document
stringByAppendingPathComponent:@"shuzu.plist"];

   
//
获取路径

   
NSArray
*array = [NSArray

arrayWithContentsOfFile:arrPath];

   
NSLog(@"%@",
array);

   

   
//
读取字典

   
//
获取路径

   
NSString
*dicPath = [document
stringByAppendingPathComponent:@"zidian.plist"];

   
NSDictionary
*dic = [NSDictionary

dictionaryWithContentsOfFile:dicPath];

   
NSLog(@"%@",
dic);

   

   
//
读取data

   
NSString
*dataPath = [document
stringByAppendingPathComponent:@"data.da"];

   
NSData
*data = [NSData

dataWithContentsOfFile:dataPath];

   
//
将data转化为字符串

   
NSString
*dataStr = [[NSString

alloc]

initWithData:data

encoding:NSUTF8StringEncoding];

NSLog(@"%@",
dataStr);

}

复杂对象归档与反归档

对复杂对象进行持久化
叫做归档与反归档
(编码与解码)

创建一个model类

.

NSFileManager文件管理者

这个类
是个单例类
用来对文件夹进行操作

//
创建一个文件夹

- (void)createFile

{

    //
需求 在documents下创建一个Download文件夹

    NSArray
*documentsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);

    NSString
*doucumentPath = documentsArr[];

    //
拼接路径

    NSString
*downloadPath = [doucumentPath
stringByAppendingPathComponent:@"Download"];

    NSLog(@"%@", downloadPath);

   

    //
创建文件夹

    //
文件管理者 这个类
是个单例类
用来对文件夹进行操作

    NSFileManager
*manager = [NSFileManager
defaultManager];

   

    // withIntermediateDirectories

    //
如果yes情况下
要创建的文件
存在的话 可以对其覆盖

    //
反之 文件存在的话
不能对其覆盖
(创建失败)

   

    BOOL
isCreatFile = [manager createDirectoryAtPath:downloadPath
withIntermediateDirectories:YES
attributes:nil
error:nil];

    NSLog(@"%d", isCreatFile);

   

}

// 宏定义

#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]



#define kCachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]



//
移动文件夹

- (void)moveFile

{

    //
获取原来的路径

    NSString
*oldPath = [kDocumentPath
stringByAppendingPathComponent:@"Download"];

    //
获取新路径 libray
下的Caches
文件夹

    NSString
*newPath = [kCachesPath
stringByAppendingPathComponent:@"Download"];

    //
创建 文件管理者类的对象(单例对象)

    NSFileManager
*manager = [NSFileManager
defaultManager];

    //
移动文件夹

   BOOL
isMoved = [manager moveItemAtPath:oldPath
toPath:newPath
error:nil];

    NSLog(@"%d", isMoved);

}

//
复制文件夹

- (void)copyFile

{

   // libaray
下的Caches
文件夹 download
复制到document文件夹下

    NSString
*oldPath = [kCachesPath
stringByAppendingPathComponent:@"download"];

    NSString
*newPath = [kDocumentPath
stringByAppendingPathComponent:@"downLoad"];

    //
创建文件管理对象

    NSFileManager
*manager = [NSFileManager
defaultManager];

    //
复制

    BOOL
isCopy = [manager copyItemAtPath:oldPath
toPath:newPath
error:nil];

    NSLog(@"%d", isCopy);

}

//
删除文件夹

- (void)deleteFile

{

    //
获取要删除的路径

    NSString
*deletePath = [kDocumentPath
stringByAppendingPathComponent:@"Download"];

    //
创建文件管理对象

    NSFileManager
*manager = [NSFileManager
defaultManager];

    BOOL
isDelete = [manager removeItemAtPath:deletePath
error:nil];

    NSLog(@"%d", isDelete);

}

//
判断文件夹是否存在

- (void)isExistFile

{

    //
获取 要判断的路径

    NSString
*path = [kCachesPath
stringByAppendingPathComponent:@"download"];

    //
创建文件管理对象

    NSFileManager
*manager = [NSFileManager
defaultManager];

    BOOL
isExist = [manager isExecutableFileAtPath:path];

    NSLog(@"%d", isExist);

}



//
复杂对象归档

- (void)archiver

{

    //
初始化对象

    JJModel
*model = [[JJModel
alloc]
init];

    //
赋值对象

    model.name
= @"MJJ";

    model.age
= ;

    //
图片

    //
把一个png格式转化成data

    model.data
= UIImagePNGRepresentation([UIImage
imageNamed:@"IMG_1873"]);

    NSMutableData
*data = [NSMutableData
data];

    //
创建一个归档对象

    NSKeyedArchiver
*archiver = [[NSKeyedArchiver
alloc]
initForWritingWithMutableData:data];

    //
进行归档编码

    [archiver encodeObject:model
forKey:@"JJModel"];

    //
编码完成

    [archiver finishEncoding];

    //
实际上归档 相当于把编码完的对象保存data中

//    NSLog(@"---------%@", data);

    //
把存有复杂对象的文件data
写入文件中 进行持久化

    //
搞路径

    NSString
*dataPath = [kDocumentPath
stringByAppendingPathComponent:@"JJmodel.da"];

//    NSLog(@"=======%@", dataPath);

    //
调入写入方法

    [data writeToFile:dataPath
atomically:YES];

    //
释放归档对象

    [archiver release];
}
//
反归档
(解码的过程)

- (void)unArchiver

{

    //
搞路径

    NSString
*path = [kDocumentPath
stringByAppendingPathComponent:@"JJModel.da"];

    //
获取刚才归档的data

    NSData
*data = [NSData
dataWithContentsOfFile:path];

    //
创建 反归档对象

    NSKeyedUnarchiver
*unArchiver = [[NSKeyedUnarchiver
alloc]
initForReadingWithData:data];

   

    //
解码 返回一个对象
应该是JJModel

    //
这个key
一定要和刚才归档的时候的key一致

    JJModel
*model = [unArchiver decodeObjectForKey:@"JJModel"];

    //
反归档完成

    [unArchiver finishDecoding];

    //
释放反归档对象

    [unArchiver release];

    NSLog(@"%@", model);

    UIImage
*image = [UIImage
imageWithData:model.data];

   
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS初级数据持久化 沙盒机制 归档与反归档的更多相关文章

  1. IOS中的沙盒机制

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件 ...

  2. IOS 沙盒机制 浅析

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件 ...

  3. 【iOS知识学习】_iOS沙盒机制

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序仅仅能在为该应用创建的目录内读取文件,不能够訪问其它地方的内容.全部的非代码文件都保存在这个地方.比方图片.声音.属性列表和文本文件 ...

  4. iOS沙盒机制介绍

    一.沙盒机制 沙盒的概念:沙盒是每一个iOS应用程序都会自动创建的一个文件系统目录(文件夹),而且沙盒还具有独立.封闭.安全的特点. 沙盒机制 iOS中的沙盒不仅仅是一个文件目录,TA其实更是一种安全 ...

  5. iOS 阶段学习第25天笔记(iOS沙盒机制介绍)

    iOS学习(OC语言)知识点整理 一.iOS沙盒机制介绍 1)概念: 每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 系统隔离,ios系统不允许访问 其他应用的应用沙盒 ...

  6. iOS沙盒机制介绍,Block 的介绍

    一.iOS沙盒机制介绍 (转载) 1)概念:每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 系统隔离,ios系统不允许访问 其他应用的应用沙盒,但在ios8中已经开放访 ...

  7. IOS 学习之 iOS沙盒(sandbox) 介绍 沙盒机制 文件操作(一)

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

  8. iOS-数据持久化基础-沙盒机制

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

  9. iOS之沙盒机制和如何获取沙盒路径

    iOS APP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URL Scheme.沙盒里面的文件可以是照片.声音文件. ...

随机推荐

  1. HDU 4417 Super Mario(划分树+二分)

    题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...

  2. 原来还有这样的记词方法_Java版记不规则动词_博主推荐

    昨天在看一本英语书的不规则动词的时候,突然产生的灵感:就是想把这样记单词简单方式,用程序代码实现,然后,使用户可以与之进行交互 这样,在用户背不规则动词的时候就会轻松把它给记住.基于这一点,于是我就思 ...

  3. (转)微信公众平台开发之基于百度 BAE3.0 的开发环境搭建(采用 Baidu Eclipse)

    原文传送门(http://blog.csdn.net/bingtianxuelong/article/details/17843111) 版本说明:     V1:         2014-2-13 ...

  4. 像jQuery那样,采用链式方法,封装一个方法:CSS()

    主要思路就是:返回this对象,将所获取的操作元素放入一个数组中.在原型中添加拓展方法 <html> <head> <title></title> &l ...

  5. ITK 3.20.1 VS2010 Configuration 配置

    Download ITK 3.20.1 Download VS2010 Download CMake 3.2.0 I assume you've already installed VS2010 an ...

  6. hdu Train Problem I

    这道题是道简单的栈模拟题,只要按照真实情况用栈进行模拟即可: #include<stdio.h> #include<string.h> #include<stack> ...

  7. MS10048依旧是Windows 2003 x86 的杀器

    今天搞了个wow的游戏论坛,服务器环境是win03 x86+iis6.0+php+mysql. 提权的时候各种无奈,mysql无权限,而且没root,试了几个别的方法都不行,实在没办法的时候,用MS1 ...

  8. HDU 2091 空心三角形(模拟)

    把一个字符三角形掏空,就能节省材料成本,减轻重量,但关键是为了追求另一种视觉效果.在设计的过程中,需要给出各种花纹的材料和大小尺寸的三角形样板,通过电脑临时做出来,以便看看效果.   Input 每行 ...

  9. [听课笔记]Professor Michael Cusumano's New Book:" Strategy Rules: Five Timeless Lessons from Bill Gates, Andy Grove, and Steve Jobs"

    1. Look Forward, Reason Back Extrapolate, interpret, then tie vision to concrete actions2. Make Big ...

  10. 有两个地方,用到了javabean对象和属性字符串值之间的转换

    1.有两个地方,用到了javabean对象和属性字符串值之间的转换 2.一个是接入层spring mvc,将json字符串参数转换为javaBean.通过@RequestBody javaBean方式 ...