您的位置:首页 > 其它

对象类型的本地写入---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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: