iOS初级数据持久化 沙盒机制 归档与反归档
来源:互联网 发布:mac版本炒股软件 编辑:程序博客网 时间:2024/05/17 09:02
数据持久化就是数据保存成文件,存储到程序中的沙盒中.
沙盒构成
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);
NSString *cachespath = cachesPatharray[0];
NSLog(@"%@", cachespath);
// 打印temp文件夹
// 该文件夹一般存储 临时文件夹
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"%@", tempPath);
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:pathatomically:YESencoding:NSUTF8StringEncodingerror:nil];
NSLog(@"%@", path);
// 简单拼接对象写入步骤
// 1.拼接要写入的路径(注意的路径一定要拼接对)
// 2. 调用写方法完事
// 写入一个数组 shuzu.plist
// 必须给后缀类型 你不给呢 就默认是text格式
NSString *arrPath = [document stringByAppendingPathComponent:@"shuzu.plist"];
NSArray *array = @[@"永乐",@"永飞",@"哈哈"];
// 调用写入方法
[array writeToFile:arrPathatomically:YES];
NSLog(@"%@", arrPath);
// 写入一个字典 zidian.plist
NSString *dicPath = [document stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary *dic = @{@"name":@"xiaofang"};
[dic writeToFile:dicPathatomically:YES];
NSLog(@"%@", dicPath);
// data的写入后缀.da
NSString *dataPath = [document stringByAppendingPathComponent:@"data.da"];
NSString *dataStr = @"你猜我是谁";
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
// 写入文件
[data writeToFile:dataPathatomically:YES];
NSLog(@"%@", dataPath);
// 复杂对象
// 自定义的类 比如person
}
//读取写入的文件
- (void)readingFile
{
// 读字符串
// 获取路径
NSArray *documentsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *document = documentsArray[0];
NSString *path = [document stringByAppendingString:@"/xiaoshuo.text"];
// 从路径中读取字符串
NSString *str = [[NSStringalloc]initWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:nil];
NSLog(@"%@", str);
// 读取数组的文件
NSString *arrPath = [document stringByAppendingPathComponent:@"shuzu.plist"];
// 获取路径
NSArray *array = [NSArrayarrayWithContentsOfFile:arrPath];
NSLog(@"%@", array);
// 读取字典
// 获取路径
NSString *dicPath = [document stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary *dic = [NSDictionarydictionaryWithContentsOfFile:dicPath];
NSLog(@"%@", dic);
// 读取data
NSString *dataPath = [document stringByAppendingPathComponent:@"data.da"];
NSData *data = [NSDatadataWithContentsOfFile:dataPath];
// 将data转化为字符串
NSString *dataStr = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];
//注意 :如果你写入字典或者数组那么数组字典中存储的数据必须是简单对象 无法写入复杂对象
- (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:pathatomically:YESencoding:NSUTF8StringEncodingerror:nil];
NSLog(@"%@", path);
// 简单拼接对象写入步骤
// 1.拼接要写入的路径(注意的路径一定要拼接对)
// 2. 调用写方法完事
// 写入一个数组 shuzu.plist
// 必须给后缀类型 你不给呢 就默认是text格式
NSString *arrPath = [document stringByAppendingPathComponent:@"shuzu.plist"];
NSArray *array = @[@"永乐",@"永飞",@"哈哈"];
// 调用写入方法
[array writeToFile:arrPathatomically:YES];
NSLog(@"%@", arrPath);
// 写入一个字典 zidian.plist
NSString *dicPath = [document stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary *dic = @{@"name":@"xiaofang"};
[dic writeToFile:dicPathatomically:YES];
NSLog(@"%@", dicPath);
// data的写入后缀.da
NSString *dataPath = [document stringByAppendingPathComponent:@"data.da"];
NSString *dataStr = @"你猜我是谁";
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
// 写入文件
[data writeToFile:dataPathatomically:YES];
NSLog(@"%@", dataPath);
// 复杂对象
// 自定义的类 比如person
}
//读取写入的文件
- (void)readingFile
{
// 读字符串
// 获取路径
NSArray *documentsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *document = documentsArray[0];
NSString *path = [document stringByAppendingString:@"/xiaoshuo.text"];
// 从路径中读取字符串
NSString *str = [[NSStringalloc]initWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:nil];
NSLog(@"%@", str);
// 读取数组的文件
NSString *arrPath = [document stringByAppendingPathComponent:@"shuzu.plist"];
// 获取路径
NSArray *array = [NSArrayarrayWithContentsOfFile:arrPath];
NSLog(@"%@", array);
// 读取字典
// 获取路径
NSString *dicPath = [document stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary *dic = [NSDictionarydictionaryWithContentsOfFile:dicPath];
NSLog(@"%@", dic);
// 读取data
NSString *dataPath = [document stringByAppendingPathComponent:@"data.da"];
NSData *data = [NSDatadataWithContentsOfFile:dataPath];
// 将data转化为字符串
NSString *dataStr = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];
NSLog(@"%@", dataStr);
}
复杂对象归档与反归档 对复杂对象进行持久化 叫做归档与反归档 (编码与解码)创建一个model类
.
NSFileManager文件管理者这个类 是个单例类 用来对文件夹进行操作
//创建一个文件夹
- (void)createFile
{
// 需求 在documents下创建一个Download文件夹
NSArray *documentsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doucumentPath = documentsArr[0];
// 拼接路径
NSString *downloadPath = [doucumentPath stringByAppendingPathComponent:@"Download"];
NSLog(@"%@", downloadPath);
// 创建文件夹
// 文件管理者 这个类是个单例类 用来对文件夹进行操作
NSFileManager *manager = [NSFileManagerdefaultManager];
// withIntermediateDirectories
// 如果yes情况下要创建的文件 存在的话 可以对其覆盖
// 反之 文件存在的话不能对其覆盖 (创建失败)
BOOL isCreatFile = [manager createDirectoryAtPath:downloadPathwithIntermediateDirectories:YESattributes:nilerror:nil];
NSLog(@"%d", isCreatFile);
}
// 宏定义
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
#define kCachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
// 移动文件夹
- (void)moveFile
{
// 获取原来的路径
NSString *oldPath = [kDocumentPathstringByAppendingPathComponent:@"Download"];
// 获取新路径 libray 下的Caches 文件夹
NSString *newPath = [kCachesPathstringByAppendingPathComponent:@"Download"];
// 创建 文件管理者类的对象(单例对象)
NSFileManager *manager = [NSFileManagerdefaultManager];
// 移动文件夹
BOOL isMoved = [manager moveItemAtPath:oldPathtoPath:newPatherror:nil];
NSLog(@"%d", isMoved);
}
// 复制文件夹
- (void)copyFile
{
// libaray 下的Caches 文件夹 download 复制到document文件夹下
NSString *oldPath = [kCachesPathstringByAppendingPathComponent:@"download"];
NSString *newPath = [kDocumentPathstringByAppendingPathComponent:@"downLoad"];
// 创建文件管理对象
NSFileManager *manager = [NSFileManagerdefaultManager];
// 复制
BOOL isCopy = [manager copyItemAtPath:oldPathtoPath:newPatherror:nil];
NSLog(@"%d", isCopy);
}
// 删除文件夹
- (void)deleteFile
{
// 获取要删除的路径
NSString *deletePath = [kDocumentPathstringByAppendingPathComponent:@"Download"];
// 创建文件管理对象
NSFileManager *manager = [NSFileManagerdefaultManager];
BOOL isDelete = [manager removeItemAtPath:deletePatherror:nil];
NSLog(@"%d", isDelete);
}
// 判断文件夹是否存在
- (void)isExistFile
{
// 获取 要判断的路径
NSString *path = [kCachesPathstringByAppendingPathComponent:@"download"];
// 创建文件管理对象
NSFileManager *manager = [NSFileManagerdefaultManager];
BOOL isExist = [manager isExecutableFileAtPath:path];
NSLog(@"%d", isExist);
}
// 复杂对象归档
- (void)archiver
{
// 初始化对象
JJModel *model = [[JJModel alloc] init];
// 赋值对象
model.name = @"MJJ";
model.age = 49;
// 图片
// 把一个png格式转化成data
model.data = UIImagePNGRepresentation([UIImageimageNamed:@"IMG_1873"]);
NSMutableData *data = [NSMutableDatadata];
// 创建一个归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
// 进行归档编码
[archiver encodeObject:modelforKey:@"JJModel"];
// 编码完成
[archiver finishEncoding];
// 实际上归档 相当于把编码完的对象保存data中
// NSLog(@"---------%@", data);
// 把存有复杂对象的文件data 写入文件中 进行持久化
// 搞路径
NSString *dataPath = [kDocumentPathstringByAppendingPathComponent:@"JJmodel.da"];
// NSLog(@"=======%@", dataPath);
// 调入写入方法
[data writeToFile:dataPathatomically:YES];
// 释放归档对象
[archiver release];
- (void)createFile
{
// 需求 在documents下创建一个Download文件夹
NSArray *documentsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doucumentPath = documentsArr[0];
// 拼接路径
NSString *downloadPath = [doucumentPath stringByAppendingPathComponent:@"Download"];
NSLog(@"%@", downloadPath);
// 创建文件夹
// 文件管理者 这个类是个单例类 用来对文件夹进行操作
NSFileManager *manager = [NSFileManagerdefaultManager];
// withIntermediateDirectories
// 如果yes情况下要创建的文件 存在的话 可以对其覆盖
// 反之 文件存在的话不能对其覆盖 (创建失败)
BOOL isCreatFile = [manager createDirectoryAtPath:downloadPathwithIntermediateDirectories:YESattributes:nilerror:nil];
NSLog(@"%d", isCreatFile);
}
// 宏定义
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
#define kCachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
// 移动文件夹
- (void)moveFile
{
// 获取原来的路径
NSString *oldPath = [kDocumentPathstringByAppendingPathComponent:@"Download"];
// 获取新路径 libray 下的Caches 文件夹
NSString *newPath = [kCachesPathstringByAppendingPathComponent:@"Download"];
// 创建 文件管理者类的对象(单例对象)
NSFileManager *manager = [NSFileManagerdefaultManager];
// 移动文件夹
BOOL isMoved = [manager moveItemAtPath:oldPathtoPath:newPatherror:nil];
NSLog(@"%d", isMoved);
}
// 复制文件夹
- (void)copyFile
{
// libaray 下的Caches 文件夹 download 复制到document文件夹下
NSString *oldPath = [kCachesPathstringByAppendingPathComponent:@"download"];
NSString *newPath = [kDocumentPathstringByAppendingPathComponent:@"downLoad"];
// 创建文件管理对象
NSFileManager *manager = [NSFileManagerdefaultManager];
// 复制
BOOL isCopy = [manager copyItemAtPath:oldPathtoPath:newPatherror:nil];
NSLog(@"%d", isCopy);
}
// 删除文件夹
- (void)deleteFile
{
// 获取要删除的路径
NSString *deletePath = [kDocumentPathstringByAppendingPathComponent:@"Download"];
// 创建文件管理对象
NSFileManager *manager = [NSFileManagerdefaultManager];
BOOL isDelete = [manager removeItemAtPath:deletePatherror:nil];
NSLog(@"%d", isDelete);
}
// 判断文件夹是否存在
- (void)isExistFile
{
// 获取 要判断的路径
NSString *path = [kCachesPathstringByAppendingPathComponent:@"download"];
// 创建文件管理对象
NSFileManager *manager = [NSFileManagerdefaultManager];
BOOL isExist = [manager isExecutableFileAtPath:path];
NSLog(@"%d", isExist);
}
// 复杂对象归档
- (void)archiver
{
// 初始化对象
JJModel *model = [[JJModel alloc] init];
// 赋值对象
model.name = @"MJJ";
model.age = 49;
// 图片
// 把一个png格式转化成data
model.data = UIImagePNGRepresentation([UIImageimageNamed:@"IMG_1873"]);
NSMutableData *data = [NSMutableDatadata];
// 创建一个归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
// 进行归档编码
[archiver encodeObject:modelforKey:@"JJModel"];
// 编码完成
[archiver finishEncoding];
// 实际上归档 相当于把编码完的对象保存data中
// NSLog(@"---------%@", data);
// 把存有复杂对象的文件data 写入文件中 进行持久化
// 搞路径
NSString *dataPath = [kDocumentPathstringByAppendingPathComponent:@"JJmodel.da"];
// NSLog(@"=======%@", dataPath);
// 调入写入方法
[data writeToFile:dataPathatomically:YES];
// 释放归档对象
[archiver release];
}
//反归档 (解码的过程)
- (void)unArchiver
{
// 搞路径
NSString *path = [kDocumentPathstringByAppendingPathComponent:@"JJModel.da"];
// 获取刚才归档的data
NSData *data = [NSData dataWithContentsOfFile:path];
// 创建 反归档对象
NSKeyedUnarchiver*unArchiver = [[NSKeyedUnarchiveralloc]initForReadingWithData:data];
// 解码 返回一个对象应该是JJModel
// 这个key 一定要和刚才归档的时候的key一致
JJModel *model = [unArchiver decodeObjectForKey:@"JJModel"];
// 反归档完成
[unArchiver finishDecoding];
// 释放反归档对象
[unArchiver release];
NSLog(@"%@", model);
UIImage *image = [UIImage imageWithData:model.data];
- (void)unArchiver
{
// 搞路径
NSString *path = [kDocumentPathstringByAppendingPathComponent:@"JJModel.da"];
// 获取刚才归档的data
NSData *data = [NSData dataWithContentsOfFile:path];
// 创建 反归档对象
NSKeyedUnarchiver*unArchiver = [[NSKeyedUnarchiveralloc]initForReadingWithData:data];
// 解码 返回一个对象应该是JJModel
// 这个key 一定要和刚才归档的时候的key一致
JJModel *model = [unArchiver decodeObjectForKey:@"JJModel"];
// 反归档完成
[unArchiver finishDecoding];
// 释放反归档对象
[unArchiver release];
NSLog(@"%@", model);
UIImage *image = [UIImage imageWithData:model.data];
}
0 0
- iOS初级数据持久化 沙盒机制 归档与反归档
- 初级数据持久化 - 沙盒机制 归档 与反归档
- iOS_初级数据持久化_归档与反归档
- iOS 数据持久化(归档与反归档)
- IOS之数据持久化(归档与反归档)
- iOS之数据持久化 - 归档与反归档
- 归档与反归档(ios数据持久化)
- 沙盒机制 简单数据和复杂数据的数据持久化 (归档反归档)
- iOS中的数据持久化,归档和反归档
- iOS数据持久化,写入,归档和反归档
- iOS 数据持久化方式 - 归档 反归档
- 数据持久化,归档,反归档
- IOS之数据持久化二(归档与反归档)
- iOS经典讲解之复杂对象的数据持久化-归档与反归档
- IOS 数据持久化只归档与取消归档
- 【学习ios之路:UI系列】iOS沙盒机制,文件读取,归档与反归档
- 【学习ios之路:UI系列】iOS沙盒机制,文件读取,归档与反归档
- iOS基础——数据存储之沙盒机制、Plist、Perference、归档、反归档
- Java基础---多线程
- oracle部署中的三层结构
- javascript倒排列子元素
- 对水波特效实现原理的解释
- 以前,现在,以后
- iOS初级数据持久化 沙盒机制 归档与反归档
- Final用法
- Objective-C 基础知识之(八):NSSet
- javascript基本数据类型与值类型引用类型说明
- 设计模式-装饰者模式
- Android多媒体---SurfaceView(播放视频)+Mediaplayer(音频)
- iOS学习 用代码实现界面
- android SDK 升级24.0.2的方法总结
- 关于Adaboost强分类器的训练