***************plist存储
// 当点点击保存的时候调用 //保存
- (IBAction)save:(id)sender {
// 获取沙盒的根路径
// NSString *home = NSHomeDirectory(); // 拼接Documents路径
// NSString *docPath = [home stringByAppendingString:@"/Documents"];
//NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; /**
* NSDocumentDirectory : 查找Documents文件夹
NSUserDomainMask : 在用户的应用程序下查找
YES 把路径展开 NO 当前应用的根路径 == ~
NO ~/Documents */
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[];
NSLog(@"%@",docPath); // 拼接文件路径
NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"]; // 只有具备writeToFile:的对象才能使用plist存储,NSArray
NSArray *array = @[@,@,@""]; [array writeToFile:filePath atomically:YES]; //NSLog(@"%@",docPath); } //当点击读取的时候调用 读取
- (IBAction)read:(id)sender {
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[];
NSLog(@"%@",docPath); // 拼接文件路径
NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"]; NSArray *data = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"%@",data); }
********篇好设置
// 保存
- (IBAction)save:(id)sender { // [NSUserDefaults standardUserDefaults]可以直接操作偏好设置文件夹
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // 自动帮我们生成一个plist文件存放在偏好设置的文件夹
[defaults setObject:@"hm" forKey:@"account"]; // 同步:把内存中的数据和沙盒同步
[defaults synchronize]; } //读取
- (IBAction)read:(id)sender {
// [NSUserDefaults standardUserDefaults]可以直接操作偏好设置文件夹
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSLog(@"%@",[defaults objectForKey:@"account"]); // NSDictionary *dict = @{@"account":@"hm"};
//
// dict writeToFile:<#(NSString *)#> atomically:<#(BOOL)#> }
***************对象归档
******persion.m
#import "HMPerson.h" @implementation HMPerson // 在对象归档的时候调用
// 哪些属性需要归档
// 这些属性怎么归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:_age forKey:@"age"];
} // 在对象解档的时候调用
// 哪些属性需要解档
// 这些属性怎么解档 - (id)initWithCoder:(NSCoder *)aDecoder
{
// 当父类实现了NSCoding,就能调用 [super initWithCoder]
if (self = [super init]) {
_age = [aDecoder decodeIntForKey:@"age"];
}
return self;
} @end
****归档的保存和读取
//保存
@implementation HMViewController - (IBAction)save:(id)sender {
HMPerson *person = [HMPerson new];
person.age = ; NSString *docPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[]; // 拼接文件路径
NSString *filePath = [docPath stringByAppendingPathComponent:@"person.data"]; [NSKeyedArchiver archiveRootObject:person toFile:filePath]; }
//读取
- (IBAction)read:(id)sender {
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[]; // 拼接文件路径
NSString *filePath = [docPath stringByAppendingPathComponent:@"person.data"]; HMPerson *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%d",p.age);
}