/**
* 缓存model 的方法 。
*/
TestModelObject *testModel = [TestModelObjectmodelWithDictionary:dict];
NSMutableArray * dataArray = [NSMutableArrayarrayWithCapacity:50];
//将testModel类型变为NSData类型
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:testModel];
//存放数据的数组将data加入进去
[dataArray addObject:data];
//记住要转换成不可变数组类型
NSArray * array = [NSArray arrayWithArray:dataArray];
NSUserDefaults *user = [NSUserDefaultsstandardUserDefaults];
[user setObject:array forKey:@"testModelArray"];
NSArray *resultArray = [user objectForKey:@"testModelArray"];
TestModelObject *resultModel = [NSKeyedUnarchiver unarchiveObjectWithData:[resultArray objectAtIndex:0]];
NSLog(@"====%@,%d",resultModel.girl1,resultModel.age);
NSMutableData *data = [NSMutableDatadata];
NSKeyedArchiver *archiver = [[NSKeyedArchiveralloc] initForWritingWithMutableData:data];
[archiver encodeObject:self.myDataArrayforKey:@"myArchiverDataKey"];
[archiver finishEncoding];
if ([ToolessaveFileToLoc:@"myFriendsList"theFile:data]) {
DDLogDebug(@"保存成");
}else{
DDLogDebug(@"保存失败");
}
dispatch_async(dispatch_get_main_queue(), ^{
[wSelf myReloadTableView];
});
});
}else{
NSData *myData = [NSDatadata];
self.myFriendListData = [NSDatadataWithData:[ToolesgetDataFileFromLoc:@"myFriendsList"into:myData]];
if (self.myFriendListData.length > 0) {
DDLogDebug(@"获取成功");
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData:self.myFriendListData];
self.myDataArray = [unarchiver decodeObjectForKey:@"myArchiverDataKey"];
}else{
DDLogDebug(@"获取失败.");
}
#pragma mark - 下面两个方法可以存储自定义的对象---TMCache就不行。
+(BOOL)saveFileToLoc:(NSString *) fileName theFile:(id) file{
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *CachePath = [fileName stringByReplacingOccurrencesOfString: @"/"withString: @"_"];
NSString *filename=[Path stringByAppendingPathComponent:CachePath];
NSFileManager *fileManager = [NSFileManagerdefaultManager];
if (![fileManager fileExistsAtPath:filename]) {
if (! [fileManager createFileAtPath:filename contents:nilattributes:nil]) {
NSLog(@"createFile error occurred");
}
}
return [file writeToFile:filename atomically:YES];
}
+(BOOL) getFileFromLoc:(NSString*)filePath into:(id)file {
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *CachePath = [filePath stringByReplacingOccurrencesOfString: @"/"withString: @"_"];
NSString *filename=[Path stringByAppendingPathComponent:CachePath];
if ([file isKindOfClass:[NSMutableDictionaryclass]]) {
[file setDictionary: [NSMutableDictionarydictionaryWithContentsOfFile:filename]];
if ([file count]==0) {
returnNO;
}
}elseif ([file isKindOfClass:[NSMutableArrayclass]]) {
[file addObjectsFromArray: [NSMutableArrayarrayWithContentsOfFile:filename]];
if ([file count]==0) {
returnNO;
}
}elseif ([file isKindOfClass:[NSDataclass]]) {
file = [NSDatadataWithContentsOfFile:filename];
if ([file length] ==0) {
returnNO;
}
}
returnYES;
}
+(NSData *) getDataFileFromLoc:(NSString*)filePath into:(id)file {
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *CachePath = [filePath stringByReplacingOccurrencesOfString: @"/"withString: @"_"];
NSString *filename=[Path stringByAppendingPathComponent:CachePath];
if ([file isKindOfClass:[NSDataclass]]) {
file = [NSDatadataWithContentsOfFile:filename];
if ([file length] ==0) {
returnnil;
}
return file;
}
returnnil;
}
方法1,archiveRootObject:toFile,它只能把1个对象写入到同一个文件中。
方法2,通过使用 NSMutableData和NSKeyedArchiver,可以将多个对象写入到同一个文件中。例如
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:testArray1 forKey:@"testArray1"];
[archiver encodeObject:testArray2 forKey:@"testArray2"];
[archiver finishEncoding];
[data writeToFile:path atomically:YES];
一:数据存储方式不同一个是序列化和反序列化后存储文件,另一个就是直接的存储文件了。
二:对象不同,archiveRootObject:toFile:可以将IOS常见的NSData,NSArray等写入文件,也可以将你自己定义的类型(必须实现了序列和凡序列化的,即遵循NSCoding协议,encodeWithCoder和initWithCoder:方法)写入文件,而writeToFile只能将NSDate等IOS常见的数据类型存入文件,因为本身遵循NSCoding协议。
#pragma mark - 下面两个方法可以存储自定义的对象---TMCache就不行。
+(BOOL)saveFileToLoc:(NSString *) fileName theFile:(id) file{
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *CachePath = [fileName stringByReplacingOccurrencesOfString: @"/"withString: @"_"];
NSString *filename=[Path stringByAppendingPathComponent:CachePath];
NSFileManager *fileManager = [NSFileManagerdefaultManager];
if (![fileManager fileExistsAtPath:filename]) {
if (! [fileManager createFileAtPath:filename contents:nilattributes:nil]) {
NSLog(@"createFile error occurred");
}
}
return [file writeToFile:filename atomically:YES];
}
+(BOOL) getFileFromLoc:(NSString*)filePath into:(id)file {
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *CachePath = [filePath stringByReplacingOccurrencesOfString: @"/"withString: @"_"];
NSString *filename=[Path stringByAppendingPathComponent:CachePath];
if ([file isKindOfClass:[NSMutableDictionaryclass]]) {
[file setDictionary: [NSMutableDictionarydictionaryWithContentsOfFile:filename]];
}elseif ([file isKindOfClass:[NSMutableArrayclass]]) {
[file addObjectsFromArray: [NSMutableArrayarrayWithContentsOfFile:filename]];
}elseif ([file isKindOfClass:[NSDataclass]]) {
file = [NSDatadataWithContentsOfFile:filename];
}
if ([file count]==0) {
returnNO;
}
returnYES;
}
相关推荐
记录一下吧,确实学到了很...它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和
FMPVC是一种工具,可通过创建一组文本文件来帮助FileMaker开发人员,这些文本文件表示数据库中的设计对象(例如脚本,自定义功能,布局等)。 fmpvc无法访问数据库内容。 命令fmpvc解析FileMaker Pro Advanced生成...
png块文本 创建或解析PNG tEXt块,以将未压缩的文本数据存储在PNG图像中。 可以与和结合使用,以添加和读取PNG图像中的自定义元数据。 在Node或使用的浏览器中工作。用法chunk = text.encode(key, value) 返回一个块...
3.7 创建自定义对象 3.7.1 创建对象 3.7.2 定义属性 3.7.3 定义方法 3.7.4 循环对象 3.7.5 获得对象的类型 3.8 操作可视TextField对象 3.8.1 实例名 3.8.2 代码提示 3.8.3 属性 3.9 响应TextField对象的用户事件 ...
TXT 文本存储)、阅读主题切换、多爬虫源自动采集和更新数据、会员充值、订阅模式、新闻发布和实时统计报表。 技术 说明 Spring Boot Spring 应用快速开发脚手架 MyBatis 持久层 ORM 框架 MyBatis Dynamic SQL ...
对于python,一切事物都是对象,程序中存储的所有数据都是对象,对象基于类创建 计算机能处理的远不止数值,还可以处理文本、图形、音频、视频、网页等各种各样的数据,不同的数据,需要定义不同的数据类型。 class ...
特征选拔搜寻中创建(也在弹出窗口中) 默认图片头像自定义图片头像选择项上的第二个文本字段自定义类名用法查看测试项目可能对了解如何设置小部件很有帮助。 窗口小部件需要一个非持久性的辅助对象才能起作用。 该...
预览地址: : 文档地址: : 系统特色内存缓存(免安装)内存数据库(免安装)个性化配置自定义目录的自述文件自定义JS,CSS文件夹密码支持在线浏览文本文件,视频,图片,音乐。 (支持FLV和HLS)文件/目录二维码...
那么就使用自定义的数据文件格式?传统情况下,我们会用一个或者若干个struct将数据打包,一下子塞进文件。但是现在用户说了:我们现在定义的数据结构可能会变:) 看来,我们的存储方案还必须要足够的柔性化。 想到...
获取其他对象的字符串表示形式 连接字符串 在字符串中查找子字符串和模式 转换字符串的大小写 字符串示例:ASCII图表 第章:使用数组 数组基础知识 索引数组 关联数组 多维数组 克隆数组 扩展Array类 数组示例:播放...
5.创建自定义对象 229 6.创建类 229 7.变量声明 229 8.定义类的属性 230 9.创建PROPERTY GET过程 230 10.创建PROPERTY LET过程 231 11.创建类方法 232 12.创建类的示例 232 13.类模块里的事件过程 232 14.创建用户...
5.1.2 主题存储在哪里 88 5.1.3 主题、版式和“幻灯片母版”视图 89 5.2 更改幻灯片版式 90 5.3 应用主题 92 5.3.1 应用主题库中的主题 92 5.3.2 应用主题或模板文件中的主题 93 5.3.3 为新演示文稿...
5.1.2 主题存储在哪里 88 5.1.3 主题、版式和“幻灯片母版”视图 89 5.2 更改幻灯片版式 90 5.3 应用主题 92 5.3.1 应用主题库中的主题 92 5.3.2 应用主题或模板文件中的主题 93 5.3.3 为新演示文稿...