For iOS development, you have to worry about data storage-dry goods, ios dry goods

Source: Internet
Author: User

For iOS development, you have to worry about data storage-dry goods, ios dry goods

1. preference settings-mostly used for user name, automatic login, and other data storage

Many iOS apps support preference settings, such as saving user names, passwords, and font sizes. iOS provides a standard solution to add preference settings to apps.

Each application has an NSUserDefaults instance (with only one switch) to access preference settings, such as saving the user name, font size, and automatic logon.

(1) Save content settings

1 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
// Store the username 2 [defaults setObject: @ "JFCK" forKey: @ "username"];
// Store the font size 3 [defaults setFloat: 18366f forKey: @ "text_size"];

(2) read the last saved setting

1 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 2 // read the value of the key username 3 NSString * username = [defaults stringForKey: @ "username"]; 4 // read the value of the key text_size
5 float textSize = [defaults floatForKey: @ "text_size"];
6 // read the value of the key auto_login.
7 BOOL autoLogin = [defaults boolForKey: @ "auto_login"];

(3) Note: When UserDefaults sets data, it does not write immediately, but regularly writes the cached data to the local disk according to the timestamp. Therefore, after the set method is called, data may be terminated without being written to the disk application. If the preceding problem occurs, you can call the synchornize method to forcibly write data.

1 [defaults synchornize];

 

2. plist storage-dictionary and Array Storage

(1) Data Storage:

 

1] obtain the Cache file path:

 

// Obtain the Cache file path

 

// NSSearchPathDirectory: directory to be searched

 

// NSSearchPathDomainMask: The search range is NSUserDomainMask, which indicates searching on the user's mobile phone

 

// Whether expandTilde expands the full path. If not, the sandbox path of the application is ~

 

// Expand the storage path

 

NSString * cachePaht = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) [0];

 

 

2] splicing File Name:

 

NSString * filePath = [cachePaht stringByAppendingPathComponent: @ "Arr. plist"];

 

 3. Write information to a file:

 

// File: full File path

 

[Arr writeToFile: filePath atomically: YES];

 

Example:

 

1-(void) saver {2 // determine whether an object can use Plist. check whether there is writeToFile 3 NSArray * arr = @ [@ "123", @ 1]. 4 // obtain the Cache file path 5 // NSSearchPathDirectory: search directory 6 // NSSearchPathDomainMask: search range NSUserDomainMask: Indicates whether to expand the full path on the user's mobile phone 7 // expandTilde, if not, the sandbox path of the application is ~ 8 // for storage, expand the path 9 NSString * cachePaht = require (NSCachesDirectory, NSUserDomainMask, YES) [0]; 10 // splice the file name 11 NSString * filePath = [cachePaht stringbyappendpathcomponent: @ "Arr. plist "]; 12 // File: full File path 13 [arr writeToFile: filePath atomically: YES]; 14}

 

 

 

Data Reading:

 

1-(void) read {2 // obtain the Cache file path 3 NSString * cachePaht = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) [0]; 4 // spliced file name 5 NSString * filePath = [cachePaht stringByAppendingPathComponent: @ "arr. plist "]; 6 NSArray * arr = [NSArray arrayWithContentsOfFile: filePath]; 7 NSLog (@" % @ ", arr); 8}

 

3. NSKeyedArchiver archive (NSCoding)

If the object is of the NSString, NSDictionary, NSArray, NSData, or NSNumber type, you can use NSKeyedArchiver to archive and restore the object;

Not all objects can be archived directly using this method. Only objects that comply with the NSCoding protocol can be archived.

NSCodingProtocol has2Methods:

1. encodeWithCoder:

This method is called every time an object is archived. Generally, this method specifies how to archive each instance variable in the object. You can use encodeObject: forKey: Method to archive instance variables.

2. initWithCoder:

This method is called every time an object is restored (decoded) from a file. The decodeObject: forKey method is used to decode the instance variable of the object.

Complete instance:Access the contact name and phone number data:

1. Define attributes and declare class methods in the header file of the data model (Object Instantiation method)

 1 #import <Foundation/Foundation.h> 2  3 @interface ICKContact : NSObject<NSCoding> 4  5 @property (nonatomic, strong) NSString *name; 6  7 @property (nonatomic, strong) NSString *phone; 8  9 + (instancetype)contactWithName:(NSString *)name andPhone:(NSString *)phone;10 11 @end

2. In the data model implementation file, in addition to class methods for instantiating objects, you also need to re-implement the object methods for instantiating objects and the methods for storing attributes:

1 # import "ICKContact. h "2 3 @ implementation ICKContact 4 // instantiate the Data Model Method 5 + (instancetype) contactWithName :( NSString *) name andPhone :( NSString *) phone 6 {7 ICKContact * contact = [[ICKContact alloc] init]; 8 contact. name = name; 9 contact. phone = phone; 10 return contact; 11} 12 // method to be called to comply with the archive Protocol 13-(void) encodeWithCoder :( NSCoder *) ACO 14 {15 [ACO encodeObject: self. name forKey: @ "name"]; 16 [aCoder encodeObject: self. phone forKey: @ "phone"]; 17} 18 19 // method to be called to comply with the archive Protocol 20-(instancetype) initWithCoder :( NSCoder *) aDecoder21 {22 // whether super is also implemented (initWithCoder: aDecoder) depends on whether the parent class super complies with the NSCoding Protocol 23 if (self = [super init]) {24 self. name = [aDecoder decodeObjectForKey: @ "name"]; 25 self. phone = [aDecoder decodeObjectForKey: @ "phone"]; 26} 27 return self; 28} 29 30 @ end

 

3. Data Storage: Implemented in files for data storage

1 // obtain the full path 2 NSString * cache = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) [0]; 3 NSString * path = [cache stringByAppendingString: @ "contacts. data "]; 4 // write data in the form of an array (the underlying layer is stored as an object) 5 // self. contacts: Data Model array (omitted here) 6 [NSKeyedArchiver archiveRootObject: self. contacts toFile: path];

4. obtain data:

1 NSString *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];2 NSString *path = [cache stringByAppendingString:@"contacts.data"];3  _contacts = [NSKeyedUnarchiver  unarchiveObjectWithFile:path];

 

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.