对象类型的本地写入---plist文件创建以及读取
2016-01-19 15:13
447 查看
#pragma mark---------------------------写入-------------------------
BPOrderInfo *orderInfo = [[BPOrderInfo alloc] init];
orderInfo.productName = @"100金币";
NSString *orderStr = [NSString stringWithFormat:@"%@",orderInfo];
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 5; i++) {
[array addObject:orderStr];
}
NSString *library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
library = [library stringByAppendingFormat:@"/Caches"];
NSFileManager *manger = [NSFileManager defaultManager];
NSString *path = [library stringByAppendingPathComponent:@"TransactionReceipt"];
[manger createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
NSString *string = [NSString stringWithFormat:@"%@/%@",path,@"Array.txt"];
// [manger createFileAtPath:string contents:nil attributes:nil];
BOOL XXx = [array writeToFile:string atomically:YES];
NSLog(@"写入路径path = %@",string);
NSLog(@"写入数组array = %@",array);
#pragma mark--------------------------------取出-----------------------
NSString *library2 = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
library2 = [library2 stringByAppendingFormat:@"/Caches"];
NSString *path2= [NSString stringWithFormat:@"%@/%@/%@",library2,@"TransactionReceipt",@"Array.txt"];
NSLog(@"取出路径path = %@",path2);
NSFileManager *manger2 = [NSFileManager defaultManager];
if ([manger2 fileExistsAtPath:path2]) {
NSArray *arr = [NSArray arrayWithContentsOfFile:path2];
NSLog(@"发现本地文件");
NSLog(@"取出数组arr = %@",arr);
for (BPOrderInfo *order in arr) {
// NSLog(@"order ==== %@",order.productName);
}
}else
{
NSLog(@"未发现本地文件");
}
// [self proprtyListTest];
}
// plist文件
-(void)proprtyListTest
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"userInfo.plist"];
NSLog(@"propertyList = %@",path);
NSMutableDictionary *userDic = [NSMutableDictionary dictionary];
[userDic setObject:@"operation" forKey:@"action"];
[userDic setObject:@"chen" forKey:@"name"];
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"张",@"王", nil];
[userDic setObject:array forKey:@"user"];
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"二十四" forKey:@"江湖"];
[array addObject:dic];
[userDic writeToFile:path atomically:YES];
}
BPOrderInfo *orderInfo = [[BPOrderInfo alloc] init];
orderInfo.productName = @"100金币";
NSString *orderStr = [NSString stringWithFormat:@"%@",orderInfo];
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 5; i++) {
[array addObject:orderStr];
}
NSString *library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
library = [library stringByAppendingFormat:@"/Caches"];
NSFileManager *manger = [NSFileManager defaultManager];
NSString *path = [library stringByAppendingPathComponent:@"TransactionReceipt"];
[manger createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
NSString *string = [NSString stringWithFormat:@"%@/%@",path,@"Array.txt"];
// [manger createFileAtPath:string contents:nil attributes:nil];
BOOL XXx = [array writeToFile:string atomically:YES];
NSLog(@"写入路径path = %@",string);
NSLog(@"写入数组array = %@",array);
#pragma mark--------------------------------取出-----------------------
NSString *library2 = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
library2 = [library2 stringByAppendingFormat:@"/Caches"];
NSString *path2= [NSString stringWithFormat:@"%@/%@/%@",library2,@"TransactionReceipt",@"Array.txt"];
NSLog(@"取出路径path = %@",path2);
NSFileManager *manger2 = [NSFileManager defaultManager];
if ([manger2 fileExistsAtPath:path2]) {
NSArray *arr = [NSArray arrayWithContentsOfFile:path2];
NSLog(@"发现本地文件");
NSLog(@"取出数组arr = %@",arr);
for (BPOrderInfo *order in arr) {
// NSLog(@"order ==== %@",order.productName);
}
}else
{
NSLog(@"未发现本地文件");
}
// [self proprtyListTest];
}
// plist文件
-(void)proprtyListTest
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [documentPath stringByAppendingPathComponent:@"userInfo.plist"];
NSLog(@"propertyList = %@",path);
NSMutableDictionary *userDic = [NSMutableDictionary dictionary];
[userDic setObject:@"operation" forKey:@"action"];
[userDic setObject:@"chen" forKey:@"name"];
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"张",@"王", nil];
[userDic setObject:array forKey:@"user"];
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"二十四" forKey:@"江湖"];
[array addObject:dic];
[userDic writeToFile:path atomically:YES];
}
相关文章推荐
- Java和C#运行速度对比:Java比C#快约3倍
- 九宫格 Swift提供经典的数组和字典两种集合类型来存储集合数据,使用数组实现一个九宫格程序,
- RecyclerView使用 及 滑动时加载图片优化方案
- shareSDK新浪和微信分享错误code=204
- css
- shareSDK新浪和微信分享错误code=204
- 完整修改主机名
- GitHub 上排名前 100 的 Objective-C 项目简介
- i OS 应用内置付费功能
- 输入域名之后默认首页跳转到其他链接去
- SQL SERVER 查看数据库表的字段类型,是否允许为NULL,默认值,主键等
- Python 5.3 多重继承
- CRM 2013 Script Loading Deep Dive
- 用聚类分析做养殖户等级划分
- CRM 2013 Script Loading Deep Dive
- CRM 2013 Script Loading Deep Dive
- CRM 2013 Script Loading Deep Dive
- centOS目录结构
- centos彻底删除文件夹、文件命令
- 逗号分隔查询