iOS儲存資料字典到沙箱,ios儲存字典沙箱

來源:互聯網
上載者:User

iOS儲存資料字典到沙箱,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 @endView 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;}@endView 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 @endView 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 *)account14 {15 16 //將一個對象寫入沙箱 需要用到一個NSKeyedArchiver 自訂對象的儲存必須用這個17 [NSKeyedArchiver archiveRootObject:account toFile:YYCAccountPath];18 }19 20 /**21 * 返回帳號資訊22 *23 * @return 帳號模型(如果帳號到期,我們會返回nil)24 */25 +(YYCAccount *)account26 {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)deleteAccount40 {41 return [[NSFileManager defaultManager] removeItemAtPath:YYCAccountPath error:nil];42 43 }44 45 46 47 48 49 @endView Code

 

3.當我們的使用的使用的時候怎麼使用呢?

儲存資料  用一個字典接收伺服器返回的資料 是一個字典

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

 將返回的資料存進沙箱  這種方法必須是返回的data裡的資訊全都有值 為空白的會崩,要判斷一下

  將返回的賬戶資料存進沙箱  應該將返回的字典資料轉為模型 再存進沙箱

//轉化為資料模型  直接調用資料模型裡的載入字典的那個方法即可

 YYCAccount *account=[YYCAccount AccountStatusWithDict:data];

//儲存帳號資訊  直接匯入帳號工具類的標頭檔直接這樣寫即可:

 [YYCAccountTool saveAccount:account];

 

擷取帳號資訊  

//擷取使用者資訊帳號模型

//YYCAccount *account=[YYCAccountTool account];

 想要什麼資料就直接account.就出來了

 

//刪除所有賬戶資訊  退出登入的時候執行的操作

 [YYCAccountTool deleteAccount];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.