iOS开发11-iOS 数据持久化(简单、复杂对象的读写)
1、简单对象的文件读写
//1.简单对象的文件读写
//简单对象(NSString、NSArray、NSDictionary、NSData)
//文件写入与读取
//1.NSString
//创建字符串
NSString *s1=@"hello world.";
//获取路径
NSString *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"documents=%@",documents);
//拼接文件名
NSString *filePath=[documents stringByAppendingPathComponent:@"hello.txt"];
//写入
[s1 writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//读取
NSString *s2=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"读取=%@",s2);
//2.NSArray
//创建数组
NSArray *arr1=@[@"Jack",@"Sam",@"Tom"];
//获取路径
NSString *caches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSLog(@"caches路径=%@",caches);
//拼接文件名
NSString *filePath2=[caches stringByAppendingPathComponent:@"arr.plist"];
//写入
[arr1 writeToFile:filePath2 atomically:YES ];
//读取
NSArray *arr2=[NSArray arrayWithContentsOfFile:filePath2];
NSLog(@"arr2=%@",arr2);
//3.NSDictionary
NSDictionary *d1=[NSDictionary dictionaryWithObjectsAndKeys:@"Jack",@"First", @"Tom",@"Second",@"Kate",@"Third",nil];
NSString *tmp=NSTemporaryDirectory();
NSLog(@"tmp路径=%@",tmp);
//拼接文件名
NSString *filePath3=[tmp stringByAppendingPathComponent:@"dic.plist"];
//写入
[d1 writeToFile:filePath3 atomically:YES ];
//读取
NSDictionary *d2=[NSDictionary dictionaryWithContentsOfFile:filePath3];
NSLog(@"d2=%@",d2);
2、复杂对象的文件读写
//复杂对象(自定义对象)文件写入
//第一种写法
Person *p1=[[Person alloc]init];
p1.name=@"Jack";
p1.age=10;
//归档-反归档 本身不是数据持久化 作为一个转化步骤
//归档Person->NSData
//归档工具
//1.创建归档工具
NSMutableData *data=[NSMutableData data];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:p1 forKey:@"p1"];
[archiver finishEncoding];//结束归档
NSString *cache=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSLog(@"获取cache路径=%@",cache);
//拼接文件名
NSString *filePath4=[cache stringByAppendingPathComponent:@"person.plist"];
//写入
[data writeToFile:filePath4 atomically:YES ];
NSLog(@"data=%@",data);
//反归档Person<-NSData
NSData * data2=[NSData dataWithContentsOfFile:filePath4];
//反归档工具
NSKeyedUnarchiver *unArchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data2];
Person *p2=[unArchiver decodeObjectForKey:@"p1"];
[unArchiver finishDecoding];
NSLog(@"p2.name=%@,p2.age=%ld",p2.name,p2.age);
//第二种写法简便写法
Person *p3=[[Person alloc]init];
p3.name=@"Jane";
p3.age=18;
NSString *cache3=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *filePathp3=[cache3 stringByAppendingPathComponent:@"p3.plist"];
//归档存储
[NSKeyedArchiver archiveRootObject:p3 toFile:filePathp3];
//反归档读取
Person *p4=[NSKeyedUnarchiver unarchiveObjectWithFile:filePathp3];
NSLog(@"%@",filePathp3);
NSLog(@"p4.name=%@",p4.name);
NSLog(@"p4.age=%ld",p4.age);
3、NSUserDefaults保存轻量级本地数据
NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名、密码之类。NSUserDefaults支持的数据格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL类型。
// NSUserDefaults 全局的--保存到Preferences 存数据比较少,传值
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
//单例
[ud setValue:@"Jack" forKey:@"Male"];//有一定的几率存不进去
[ud synchronize];//同步之后才能存进去
NSLog(@"ud valueForKey:Male=%@",[ud valueForKey:@"Male"]);
代码下载(Xcode7.0.1)
有问题请联系博主,邮箱:nathanlee1987@aliyun.com
著作权声明:本文由http://my.csdn.net/Nathan1987_原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢