IOS stores data dictionary to sandbox, and ios stores dictionary sandbox

Source: Internet
Author: User

IOS stores data dictionary to sandbox, and ios stores dictionary sandbox

1. Create an account data model to store the data returned from the server. Generally, the returned data is a dictionary containing various information of the login user. This data model is used to store these items.

When creating a data model, YYCAccount inherits NSObject and must comply with the <NSCoding> protocol.

In the YYCAccount. h file, the fields in the Code are written based on the returned data. Generally, only usable fields can be written.

1 # import <Foundation/Foundation. h> 2 3 @ interface YYCAccount: NSObject <NSCoding> 4/** 5 * User ID 6 */7 @ property (nonatomic, assign) int uid; 8/** 9 * User name 10 */11 @ property (nonatomic, copy) NSString * name; 12/** 13 * mobile phone number 14 */15 @ property (nonatomic, copy) NSString * tel; 16/** 17 * Date of birth 18 */19 @ property (nonatomic, copy) NSString * birthday; 20/** 21 * Gender 22 */23 @ property (nonatomic, copy) NSString * sex; 24/** 25 * image storage directory 26 */27 @ property (nonatomic, copy) NSString * category; 28/** 29 * User password 30 */31 @ property (nonatomic, copy) NSString * password; 32/** 33 * coupon quantity 34 */35 @ property (nonatomic, assign) int counum; 36/** 37 * Love tooth index 38 */39 @ property (nonatomic, assign) int level; 40/** 41 * image name 42 */43 @ property (nonatomic, copy) NSString * filename; 44 45/** 46 * points 47 */48 @ property (nonatomic, assign) int integral; 49/** 50 * Total check-in days 51 */52 @ property (nonatomic, assign) int alldays; 53 54/** 55 * last sign-in time 56 */57 @ property (nonatomic, copy) NSString * lastCheckinTime; 58 59 60/** 61 * used to load dictionary account information 62*63 * @ param dict <# dict description #> 64*65 * @ return <# return value description #> 66 */67 + (instancetype) accountStatusWithDict: (NSDictionary *) dict; 68 69 70 71 @ endView Code

The code in the YYCAccount. m file is mainly divided into two methods: Archive and archive. Note that the storage type must be consistent with the data type, and a method for loading dictionary account information must be implemented.

# 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;}/*** the object is archived into the sandbox, this method describes the attributes of this object written into the sandbox * @ 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"]; //}/*** this method is called for file unarchiving * purpose: in this method, it shows which attributes of this object are parsed from shahe. parsing objects from shahe will call this method to parse which attributes * @ 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. Create an account storage tool class YYCAccountTool to inherit the header file of the NSObject imported data model YYCAccount

Tool-class storage accounts that process all account-related operations, retrieve accounts, and verify accounts

The. h file code of the YYCAccountTool tool class

1 # import <Foundation/Foundation. h> 2 # import "YYCAccount. h "3 @ interface YYCAccountTool: NSObject 4/** 5 * storage account information 6*7 * @ param account Model 8 */9 + (void) saveAccount :( YYCAccount *) account; 10 11/** 12 * return account information 13*14 * @ return account model (if the account expires, we will return nil) 15 */16 + (YYCAccount *) account; 17 18/** 19 * Delete account information 20*21 * @ return <# return value description #> 22 */23 + (BOOL) deleteAccount; 24 25 26 27 @ endView Code

The. m file code of the YYCAccountTool tool class. Note that the account information storage path is written as a macro, And the last part is the file name. It doesn't matter if you write it like this.

1 # import "YYCAccountTool. h "2 3 // account information storage Path 4 # define YYCAccountPath [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @" account. archive "] 5 6 7 @ implementation YYCAccountTool 8/** 9 * storage account information 10*11 * @ param account Model 12 */13 + (void) saveAccount :( YYCAccount *) account14 {15 16 // to write an object to the sandbox, you need to use the storage of an NSKeyedArchiver custom object. This 17 [NSKeyedArchiver archiveRootObject: account toFile: YYCAccountPath] must be used; 18} 19 20/** 21 * return account information 22*23 * @ return account model (if the account expires, we will return nil) 24 */25 + (YYCAccount *) account26 {27 // load model 28 YYCAccount * account = [NSKeyedUnarchiver unarchiveObjectWithFile: YYCAccountPath]; 29 30 return account; 31 32} 33 34/** 35 * Delete account information 36*37 * @ return <# return value description #> 38 */39 + (BOOL) deleteAccount40 {41 return [[NSFileManager defamanager manager] removeItemAtPath: YYCAccountPath error: nil]; 42 43} 44 46 47 48 49 @ endView Code

 

3. How can we use it when we use it?

The storage data uses a dictionary to receive the data returned by the server.

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

This method must be used to store the returned data into the sandbox. If all the information in the returned data has null values, it will crash.

Store the returned account data in the sandbox. Convert the returned Dictionary data into a model and then store it in the sandbox.

// Convert to the data model and directly call the method for loading the dictionary in the data model.

YYCAccount * account = [YYCAccount AccountStatusWithDict: data];

// Import the storage account information directly to the header file of the account tool class and write it like this:

[YYCAccountTool saveAccount: account];

 

Get account information

// Obtain the user information account Model

// YYCAccount * account = [YYCAccountTool account];

If you want any data, go to account.

 

// Delete all account information and exit the logon operation

[YYCAccountTool deleteAccount];

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.