ios存储 plist 偏好设置 自定义对象存储

1,plist

Plist注意:不能存储自定义对象

 Plist:数组和字典,
  如何判断一个对象能不能使用Plist,就看下有没有writeToFile

获取应用的文件夹(应用沙盒)

NSString* homePath=NSHomeDirectory();

获取temp

NSTemporaryDirectory();

获取Cache文件路径

NSSearchPathDirectory:搜索的目录

NSSearchPathDomainMask:搜索范围NSUserDomainMask表示在用户的手机上查找

expandTilde是否展开全路径,如果没有展开 应用沙盒的路径就~

存储一定要展开路径

获取文件存取路径,此处以cache为例

NSString* cachePath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)[0];

拼接文件名称

NSString* filePath=[cachePath stringByAppendingPathComponent:@"personArr.plist"];

File:文件全路径 存

[arr writeToFile:filePath atomically:YES]

 取

NSArray* arr=[NSArray arrayWithContentsOfFile:filePath];

 1 //
 2 //  ViewController.m
 3 //  05-PIist存储
 4 //
 5 //  Created by xiaomage on 15/6/13.
 6 //  Copyright (c) 2015年 xiaomage. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "Person.h"
11 
12 @interface ViewController ()
13 
14 @end
15 
16 @implementation ViewController
17 // 点击存储的时候调用
18 - (IBAction)save:(id)sender {
19     Person *p = [[Person alloc] init];
20     // Plist注意:不能存储自定义对象
21     // Plist:数组和字典,
22     // 如何判断一个对象能不能使用Plist,就看下有没有writeToFile
23     NSArray *arr = @[@"123",@1,p];
24 
25     
26     // 获取应用的文件夹(应用沙盒)
27 //    NSString *homePath = NSHomeDirectory();
28     
29     // 获取temp
30 //    NSTemporaryDirectory();
31     
32     // 获取Cache文件路径
33     // NSSearchPathDirectory:搜索的目录
34     // NSSearchPathDomainMask:搜索范围 NSUserDomainMask:表示在用户的手机上查找
35     // expandTilde 是否展开全路径,如果没有展开,应用的沙盒路径就是~
36     // 存储一定要要展开路径
37     NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
38     
39     
40     // 拼接文件名
41     NSString *filePath = [cachePaht stringByAppendingPathComponent:@"personArr.plist"];
42     
43     
44     
45     NSLog(@"%@",cachePaht);
46     
47     
48     // File:文件的全路径
49     [arr writeToFile:filePath atomically:YES];
50     
51 }
52 
53 - (IBAction)read:(id)sender {
54     
55     NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
56     
57     
58     // 拼接文件名
59     NSString *filePath = [cachePaht stringByAppendingPathComponent:@"arr.plist"];
60     
61    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
62     
63     NSLog(@"%@",arr);
64     
65 }
66 
67 - (void)viewDidLoad {
68     [super viewDidLoad];
69     // Do any additional setup after loading the view, typically from a nib.
70 }
71 
72 - (void)didReceiveMemoryWarning {
73     [super didReceiveMemoryWarning];
74     // Dispose of any resources that can be recreated.
75 }
76 
77 @end
View Code

2偏好设置

好处:不用关心文件名称,快速做键值对存储,

底层:就是封装了一个字典

获取偏好设置对象

NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];

[userDefaults setObject:@"lxl" forKey:@"account"];

[userDefaults setBool:YES forKey:@"rmbPwd"];

iso7之前默认不会马上把缓存跟硬盘同步

要调用 [userDefaults synchronize];

默认生成一个plist文件 plist不能存自定对象

[userDefaults objectForKey:@"account"];

[userDefaults BoolForKey:@"rmbPwd"];

 1 //
 2 //  ViewController.m
 3 //  06-偏好设置
 4 //
 5 //  Created by xiaomage on 15/6/13.
 6 //  Copyright (c) 2015年 xiaomage. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()
12 
13 @end
14 
15 @implementation ViewController
16 
17 // 偏好设置存储:
18 // 好处:1.不需要关心文件名
19 // 2.快速做键值对存储
20 
21 // 底层:就是封装了一个字典
22 // account:xmg pwd:123 rmbPwd:YES
23 - (IBAction)save:(id)sender {
24     
25     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
26     
27     [userDefaults setObject:@"xmg" forKey:@"account"];
28     [userDefaults setObject:@"123" forKey:@"pwd"];
29     [userDefaults setBool:YES forKey:@"rmbPwd"];
30     // 在iOS7之前,默认不会马上把跟硬盘同步
31     // 同步
32     [userDefaults synchronize];
33     
34     
35     
36 }
37 - (IBAction)read:(id)sender {
38    NSString *pwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"pwd"];
39     
40     NSLog(@"%@",pwd);
41     
42 }
43 
44 - (void)viewDidLoad {
45     [super viewDidLoad];
46     // Do any additional setup after loading the view, typically from a nib.
47 }
48 
49 - (void)didReceiveMemoryWarning {
50     [super didReceiveMemoryWarning];
51     // Dispose of any resources that can be recreated.
52 }
53 
54 @end
View Code

3自定义对象存储

获取文件路径

NSString * filePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)[0] stringByAppendingPathComponent:@"person.data"];

对象

Person* p=[[Person alloc]init];

[NSKeyedArchiver archiveRootObject:p toFile:filePath];

Person* p=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

一个自定义对象想要归档需要实现协议NSCoding,并实现协议的2个方法

描述对象的那些属性归档

- (void)encodeWithCoder:(NSCoder *)aCoder;

描述对象的那些属性解档

- (id)initWithCoder:(NSCoder *)aDecoder;

实现initWithCoder注意如果当前自定义类的父类没有实现NSCoding协议就不能调用super initWithCoder:

而是调 super init

通过xib,storyboard创建的页面会调用initWithCoder

通过代码创建的页面会调用initWithFrame

 1 //
 2 //  ViewController.m
 3 //  07-自定义对象归档
 4 //
 5 //  Created by xiaomage on 15/6/13.
 6 //  Copyright (c) 2015年 xiaomage. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "Person.h"
11 
12 @interface ViewController ()
13 
14 @end
15 
16 @implementation ViewController
17 - (IBAction)save:(id)sender {
18     
19     Person *p = [[Person alloc] init];
20     p.age = 18;
21     
22     // 获取cache
23     NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
24     
25     // 获取文件的全路径
26     NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
27     
28     // 把自定义对象归档
29     [NSKeyedArchiver archiveRootObject:p toFile:filePath];
30     
31 }
32 - (IBAction)read:(id)sender {
33     
34     // 获取cache
35     NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
36     
37     // 获取文件的全路径
38     NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
39     
40     // 解档
41     Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
42     
43     NSLog(@"%d",p.age);
44     
45 }
46 
47 - (void)viewDidLoad {
48     [super viewDidLoad];
49     // Do any additional setup after loading the view, typically from a nib.
50 }
51 
52 - (void)didReceiveMemoryWarning {
53     [super didReceiveMemoryWarning];
54     // Dispose of any resources that can be recreated.
55 }
56 
57 @end
View Code
 1 //
 2 //  Person.h
 3 //  07-自定义对象归档
 4 //
 5 //  Created by xiaomage on 15/6/13.
 6 //  Copyright (c) 2015年 xiaomage. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 // 如果一个自定义对象想要归档,必须遵守NSCoding协议,实现协议方法。
12 @interface Person : NSObject<NSCoding>
13 @property (nonatomic, assign) int age;
14 @property (nonatomic, strong) NSString* name;
15 @end
16 
17 
18 //
19 //  Person.m
20 //  07-自定义对象归档
21 //
22 //  Created by xiaomage on 15/6/13.
23 //  Copyright (c) 2015年 xiaomage. All rights reserved.
24 //
25 
26 #import "Person.h"
27 
28 @implementation Person
29 
30 // 什么时候调用:自定义对象归档的时候
31 
32 // 作用:用来描述当前对象里面的哪些属性需要归档
33 - (void)encodeWithCoder:(NSCoder *)aCoder
34 {
35     // name
36     [aCoder encodeObject:_name forKey:@"name"];
37     
38     // age
39     [aCoder encodeInt:_age forKey:@"age"];
40     
41 }
42 
43 
44 // 什么时候调用:解档对象的时候调用
45 
46 // 作用:用来描述当前对象里面的哪些属性需要解档
47 // initWithCoder:就是用来解析文件的。
48 - (id)initWithCoder:(NSCoder *)aDecoder
49 {
50     // super:NSObject
51 #warning 什么时候需要调用initWithCoder
52     if (self = [super init]) {
53         
54         // 注意:一定要给成员变量赋值
55         // name
56        _name = [aDecoder decodeObjectForKey:@"name"];
57         
58         // age
59        _age = [aDecoder decodeIntForKey:@"age"];
60 
61     }
62     return self;
63     
64     
65 }
66 
67 
68 @end
View Code
原文地址:https://www.cnblogs.com/jiaozi-li/p/5717985.html