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];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM