iOS存储数据字典到沙盒

1.创建一个账号数据模型 用来存放从服务器返回的数据,一般返回的是一个字典,里面包含了这个登陆用户的各种信息,这个数据模型就是用来存放这些东西的

创建一个数据模型  YYCAccount 继承 NSObject   注意要遵守<NSCoding>协议

YYCAccount.h文件中代码 这里面字段根据返回的数据写,一般写能用的上的就行了,不需要的不用写

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface YYCAccount : NSObject <NSCoding>
 4 /**
 5  *  用户ID
 6  */
 7 @property (nonatomic, assign) int uid;
 8 /**
 9  *  用户姓名
10  */
11 @property (nonatomic, copy) NSString *name;
12 /**
13  *  手机号
14  */
15 @property (nonatomic, copy) NSString *tel;
16 /**
17  *  出生日期
18  */
19 @property (nonatomic, copy) NSString *birthday;
20 /**
21  *  性别
22  */
23 @property (nonatomic, copy) NSString *sex;
24 /**
25  *  图片存放目录
26  */
27 @property (nonatomic, copy) NSString *category;
28 /**
29  *  用户密码
30  */
31 @property (nonatomic, copy) NSString *password;
32 /**
33  *  优惠券数量
34  */
35 @property (nonatomic, assign) int counum;
36 /**
37  *  爱牙指数
38  */
39 @property (nonatomic, assign) int level;
40 /**
41  *  图片名称
42  */
43 @property (nonatomic, copy) NSString *filename;
44 
45 /**
46  *  积分
47  */
48 @property (nonatomic, assign) int integral;
49 /**
50  *  签到总天数
51  */
52 @property (nonatomic, assign) int alldays;
53 
54 /**
55  *  上次签到时间
56  */
57 @property (nonatomic, copy) NSString *lastCheckinTime;
58 
59 
60 /**
61  *  用来加载字典 账户信息
62  *
63  *  @param dict <#dict description#>
64  *
65  *  @return <#return value description#>
66  */
67 +(instancetype)AccountStatusWithDict: (NSDictionary *)dict;
68 
69 
70 
71 @end
View Code

YYCAccount.m文件中代码 主要是归档 和反归档两个方法,注意存储类型要和数据类型一致  还有一个加载字典账户信息的方法要实现

#import "YYCAccount.h"

@implementation YYCAccount

+(instancetype)AccountStatusWithDict:(NSDictionary *)dict
{
    YYCAccount *account=[[self alloc]init];
    account.uid=[dict[@"uid"] intValue];
    account.name=dict[@"name"];
    account.tel=dict[@"tel"];
    account.birthday=dict[@"birthday"];
    account.filename=dict[@"filename"];

    account.counum=[dict[@"counum"] intValue];
    account.level=[dict[@"level"] intValue];
    account.integral=[dict[@"integral"] intValue];
    account.alldays=[dict[@"alldays"] intValue];
    account.lastCheckinTime=dict[@"lastCheckinTime"];
    


    return account;
}

/**
 *  当一个对象要归档进沙盒的时候就会调用  归档
 *  目的,在这个方法中说明这个对象的哪些属性写进沙盒
 *  @param encoder <#encoder description#>
 */
-(void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeInt:self.uid forKey:@"uid"];
    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeObject:self.tel forKey:@"tel"];
    [encoder encodeObject:self.birthday forKey:@"birthday"];

    [encoder encodeInteger:self.counum forKey:@"counum"];
    [encoder encodeInteger:self.level forKey:@"level"];
    [encoder encodeInteger:self.integral forKey:@"integral"];
    [encoder encodeInteger:self.alldays forKey:@"alldays"];
    [encoder encodeObject:self.lastCheckinTime forKey:@"lastCheckinTime"];
    [encoder encodeObject:self.filename forKey:@"filename"];
//
}

/**
 *  反归档 的时候会调用这个方法  解档
 *  目的:在这个方法中说明这个对象的哪些属性从沙河中解析出来
 从沙河中解析对象 反归档会调用这个方法 需要解析哪些属性
 *  @param decoder <#decoder description#>
 *
 *  @return <#return value description#>
 */
-(instancetype)initWithCoder:(NSCoder *)decoder
{
    if (self=[super init]) {
        self.uid=[decoder decodeIntForKey:@"uid"];
        self.name=[decoder decodeObjectForKey:@"name"];
        self.tel=[decoder decodeObjectForKey:@"tel"];
        self.birthday=[decoder decodeObjectForKey:@"birthday"];

        self.counum=[decoder decodeIntForKey:@"counum"];
        self.level=[decoder decodeIntForKey:@"level"];
        self.integral=[decoder decodeIntForKey:@"integral"];
        self.alldays=[decoder decodeIntForKey:@"alldays"];
        self.lastCheckinTime=[decoder decodeObjectForKey:@"lastCheckinTime"];
        self.filename=[decoder decodeObjectForKey:@"filename"];
        
    }
    return self;
}


@end
View Code

2.创建一个账号存储工具类  YYCAccountTool 继承 NSObject   导入数据模型YYCAccount的头文件

处理账号相关的所有操作的工具类 存储账号、取出账号、验证账号

YYCAccountTool工具类的.h文件代码

 1 #import <Foundation/Foundation.h>
 2 #import "YYCAccount.h"
 3 @interface YYCAccountTool : NSObject
 4 /**
 5  *  存储账号信息
 6  *
 7  *  @param account 账号模型
 8  */
 9 +(void)saveAccount:(YYCAccount *)account;
10 
11 /**
12  *  返回账号信息
13  *
14  *  @return 账号模型(如果账号过期,我们会返回nil)
15  */
16 +(YYCAccount *)account;
17 
18 /**
19  *  删除账号信息
20  *
21  *  @return <#return value description#>
22  */
23 +(BOOL)deleteAccount;
24 
25 
26 
27 @end
View Code

YYCAccountTool工具类的.m文件代码   注意账号信息存储路径 写成了一个宏,最后面是文件的名字,自己随意,一般都这样写没关系

 1 #import "YYCAccountTool.h"
 2 
 3 //账号信息存储路径
 4 #define YYCAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"]
 5 
 6 
 7 @implementation YYCAccountTool
 8 /**
 9  *  存储账号信息
10  *
11  *  @param account 账号模型
12  */
13 +(void)saveAccount:(YYCAccount *)account
14 {
15     
16     //将一个对象写入沙盒 需要用到一个NSKeyedArchiver 自定义对象的存储必须用这个
17     [NSKeyedArchiver archiveRootObject:account toFile:YYCAccountPath];
18 }
19 
20 /**
21  *  返回账号信息
22  *
23  *  @return 账号模型(如果账号过期,我们会返回nil)
24  */
25 +(YYCAccount *)account
26 {
27     //加载模型
28     YYCAccount *account=[NSKeyedUnarchiver unarchiveObjectWithFile:YYCAccountPath];
29     
30     return account;
31     
32 }
33 
34 /**
35  *  删除账号信息
36  *
37  *  @return <#return value description#>
38  */
39 +(BOOL)deleteAccount
40 {
41     return [[NSFileManager defaultManager] removeItemAtPath:YYCAccountPath error:nil];
42     
43 }
44 
45 
46 
47 
48 
49 @end
View Code

 

3.当我们的使用的使用的时候怎么使用呢?

存储数据  用一个字典接收服务器返回的数据 是一个字典

 NSDictionary *data=dict[@"data"];

 将返回的数据存进沙盒  这种方法必须是返回的data里的信息全都有值 为空的会崩,要判断一下

  将返回的账户数据存进沙盒  应该将返回的字典数据转为模型 再存进沙盒

//转化为数据模型  直接调用数据模型里的加载字典的那个方法即可

 YYCAccount *account=[YYCAccount AccountStatusWithDict:data];

//存储账号信息  直接导入账号工具类的头文件直接这样写即可:

 [YYCAccountTool saveAccount:account];

 

获取账号信息  

//获取用户信息账号模型

//YYCAccount *account=[YYCAccountTool account];

 想要什么数据就直接account.就出来了

 

//删除所有账户信息  退出登录的时候执行的操作

 [YYCAccountTool deleteAccount];

 

posted @ 2016-03-25 18:03  红街咖啡  阅读(2481)  评论(0编辑  收藏  举报