iOS数据持久化--NSArray NSDictionary NSString沙盒存储

数组

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/pathArr"];
NSURL *url = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/urlArr"]];
//数组写入沙盒
NSArray *array = @[@1,@2,@3,@4,@5,@6];
NSString *pathResult = [array writeToFile:path atomically:YES] ? @"通过path存入成功" : @"通过path存入失败";
NSString *urlResult = [array writeToURL:url atomically:YES] ? @"通过url存入成功" : @"通过url存入失败";
NSLog(@"%@\n%@",pathResult,urlResult);
//从沙盒读取数组
NSArray *array1 = [[NSArray alloc]initWithContentsOfFile:path];
NSArray *array2 = [NSArray arrayWithContentsOfFile:path];
NSArray *array3 = [[NSArray alloc]initWithContentsOfURL:url];
NSArray *array4 = [NSArray arrayWithContentsOfURL:url];
NSLog(@"%@-%@-%@-%@",array1,array2,array3,array4);

字典

NSDictionary *dict = @{@"1":@1,@"2":@2,@"3":@3,@"4":@4,@"5":@5,@"6":@6};
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/pathDict"];
NSURL *url = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/urlDict"]];
//字典写入沙盒
NSString *pathResult = [dict writeToFile:path atomically:YES] ? @"通过path存入成功" : @"通过path存入失败";
NSString *urlResult = [dict writeToURL:url atomically:YES] ? @"通过url存入成功" : @"通过url存入失败";
NSLog(@"%@\n%@",pathResult,urlResult);
//从沙盒读取数组
NSDictionary *dict1 = [[NSDictionary alloc]initWithContentsOfFile:path];
NSDictionary *dict2 = [NSDictionary dictionaryWithContentsOfFile:path];
NSDictionary *dict3 = [[NSDictionary alloc]initWithContentsOfURL:url];
NSDictionary *dict4 = [NSDictionary dictionaryWithContentsOfURL:url];
NSLog(@"%@-%@-%@-%@",dict1,dict2,dict3,dict4);

字符串

NSString *string = @"message123456";
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/pathString"];
NSURL *url = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/urlString"]];
//字典写入沙盒
NSString *pathResult = [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil] ? @"通过path存入成功" : @"通过path存入失败";
NSString *urlResult = [string writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil] ? @"通过url存入成功" : @"通过url存入失败";
NSLog(@"%@\n%@",pathResult,urlResult);
//从沙盒读取数组
//使用指定编码获取
NSString *string1 = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *string2 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *string3 = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSString *string4 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@-%@-%@-%@",string1,string2,string3,string4);

//尝试确定编码 并返回
NSStringEncoding encoding = NSUTF8StringEncoding;
NSString *string11 = [[NSString alloc]initWithContentsOfFile:path usedEncoding:&encoding error:nil];
NSString *string22 = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error:nil];
NSString *string33 = [[NSString alloc]initWithContentsOfURL:url usedEncoding:&encoding error:nil];
NSString *string44 = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:nil];
NSLog(@"%@-%@-%@-%@",string11,string22,string33,string44);

你可能感兴趣的:(iOS数据持久化--NSArray NSDictionary NSString沙盒存储)