iOS存储数据字典到沙盒

时间:2023-01-28 19:09:02

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

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

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

 #import <Foundation/Foundation.h>

 @interface YYCAccount : NSObject <NSCoding>
/**
* 用户ID
*/
@property (nonatomic, assign) int uid;
/**
* 用户姓名
*/
@property (nonatomic, copy) NSString *name;
/**
* 手机号
*/
@property (nonatomic, copy) NSString *tel;
/**
* 出生日期
*/
@property (nonatomic, copy) NSString *birthday;
/**
* 性别
*/
@property (nonatomic, copy) NSString *sex;
/**
* 图片存放目录
*/
@property (nonatomic, copy) NSString *category;
/**
* 用户密码
*/
@property (nonatomic, copy) NSString *password;
/**
* 优惠券数量
*/
@property (nonatomic, assign) int counum;
/**
* 爱牙指数
*/
@property (nonatomic, assign) int level;
/**
* 图片名称
*/
@property (nonatomic, copy) NSString *filename; /**
* 积分
*/
@property (nonatomic, assign) int integral;
/**
* 签到总天数
*/
@property (nonatomic, assign) int alldays; /**
* 上次签到时间
*/
@property (nonatomic, copy) NSString *lastCheckinTime; /**
* 用来加载字典 账户信息
*
* @param dict <#dict description#>
*
* @return <#return value description#>
*/
+(instancetype)AccountStatusWithDict: (NSDictionary *)dict; @end

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

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

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

YYCAccountTool工具类的.h文件代码

 #import <Foundation/Foundation.h>
#import "YYCAccount.h"
@interface YYCAccountTool : NSObject
/**
* 存储账号信息
*
* @param account 账号模型
*/
+(void)saveAccount:(YYCAccount *)account; /**
* 返回账号信息
*
* @return 账号模型(如果账号过期,我们会返回nil)
*/
+(YYCAccount *)account; /**
* 删除账号信息
*
* @return <#return value description#>
*/
+(BOOL)deleteAccount; @end

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

 #import "YYCAccountTool.h"

 //账号信息存储路径
#define YYCAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archive"] @implementation YYCAccountTool
/**
* 存储账号信息
*
* @param account 账号模型
*/
+(void)saveAccount:(YYCAccount *)account
{ //将一个对象写入沙盒 需要用到一个NSKeyedArchiver 自定义对象的存储必须用这个
[NSKeyedArchiver archiveRootObject:account toFile:YYCAccountPath];
} /**
* 返回账号信息
*
* @return 账号模型(如果账号过期,我们会返回nil)
*/
+(YYCAccount *)account
{
//加载模型
YYCAccount *account=[NSKeyedUnarchiver unarchiveObjectWithFile:YYCAccountPath]; return account; } /**
* 删除账号信息
*
* @return <#return value description#>
*/
+(BOOL)deleteAccount
{
return [[NSFileManager defaultManager] removeItemAtPath:YYCAccountPath error:nil]; } @end

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

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

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

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

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

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

YYCAccount *account=[YYCAccount AccountStatusWithDict:data];

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

[YYCAccountTool saveAccount:account];

获取账号信息

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

//YYCAccount *account=[YYCAccountTool account];

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

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

[YYCAccountTool deleteAccount];