6. Five data storage methods (I) and five Data Storage Methods
There are five storage methods for data in iOS development:
1. plist (XML Attribute list archiving)
2. preference settings
3. NSKeydeArchiver archive (store custom objects)
1. plist (XML Attribute list archiving) can only access object-class filesMethod 1: (paths can be retrieved from all four folders)
1 // obtain the sandbox Path 2 NSString * home = NSHomeDirectory (); 3 // you can obtain the paths of the other three folders) 4 NSString * path = [home stringByAppendingString: @ "/documents"]; 5 NSString * path1 = [home stringByAppendingPathComponent: @ "library/cache"];
Method 2: (only documents and cache can retrieve the path) (YES must be written when obtaining the path; otherwise, data cannot be written successfully)
1 // obtain the cache path. "~" is used before the file path. 2 NSString * cache = [callback (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 3 NSString * doucument = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 4 // splicing path 5 NSString * path = [cache stringByAppendingPathComponent: @ "abc. plist "]; 6 NSString * path1 = [doucument stringByAppendingPathComponent: @"/ce. plist "]; 7 // write the file to the folder 8 [arr writeToFile: path atomically: YES]; 9 [arr writeToFile: path1 atomically: YES];
Read files:
1 // read the file 2 NSArray * arr1 = [NSArray arrayWithContentsOfFile: path]; 3 NSLog (@ "% @", arr1 );
2. preference settings (Basic settings for saving user name, password, font size, automatic logon, etc.) are automatically saved to the Preferences folder in the sandbox.
1 // get object 2 NSUserDefaults * ud = [NSUserDefaults standardUserDefaults]; 3 // store data 4 [ud setObject: @ "abc" forKey: @ "what is this"]; 5 [ud setObject: @ "cde" forKey: @ ""]; 6 // read data 7 NSString * str = [ud objectForKey: @ ""]; 8 NSString * str1 = [ud objectForKey: @ "what is this"]; 9 NSLog (@ "% @ --- % @", str, str1 );
Note: During preference storage, the storage time is uncertain. It may be stored in a folder at a certain time in the future, so you can synchronize the storage immediately:
[Ud synchronize];
Iii. NSKeyedArchive archiving (storing custom objects)
In viewController. m:
1-(void) viewDidLoad {2 [super viewDidLoad]; 3 4 ZWPerson * p = [[ZWPerson alloc] init]; 5 p. name = @ "mc"; 6 p. age = 18; 7 p. height = 1.88; 8 NSString * cache = [callback (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 9 NSString * doucument = [callback (NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 10 // splicing path 11 NSString * path = [cache stringByAppendingPathComponent: @ "abc.doc"]; 12 NSString * path1 = [doucument stringByAppendingPathComponent: @ "/ce.doc"]; 13 // store data 14 BOOL ar = [NSKeyedArchiver archiveRootObject: p toFile: path]; 15 BOOL ar1 = [NSKeyedArchiver archiveRootObject: p toFile: path1]; 16 NSLog (@ "% d --- % d", ar, ar1); 17 // read data 18 ZWPerson * p1 = [NSKeyedUnarchiver unarchiveObjectWithFile: path]; 19 ZWPerson * p2 = [NSKeyedUnarchiver unarchiveObjectWithFile: path1]; 20 NSLog (@ "% zd --- % @", p1.age, p2.name); 21}
Create a ZWPerson class (inherited from ViewController)
In ZWPerson. h:
1 # import "ViewController. h "2 @ interface ZWPerson: ViewController3/** name */4 @ property (strong, nonatomic) NSString * name; 5/** age */6 @ property (assign, nonatomic) NSInteger age; 7/** height */8 @ property (assign, nonatomic) double height; 9 @ end
In ZWPerson. h:
1 # import "ZWPerson. h "2 @ interface ZWPerson () 3 @ end 4 @ implementation ZWPerson 5 // call this method when storing 6-(void) encodeWithCoder :( NSCoder *) ecoder 7 {8 // stores object attributes (stores data as needed) 9 [ecoder encodeObject: self. name forKey: @ "name"]; 10 [aCoder encodeInteger: self. age forKey: @ "age"]; 11 [ecoder encodeDouble: self. height forKey: @ "height"]; 12} 13 // call 14-(instancetype) initWithCoder :( NSCoder *) aDecoder15 {16 if (self = [super initWithCoder: aDecoder]) {. 17 // get object attributes (obtain data as needed) 18 self. name = [aDecoder decodeObjectForKey: @ "name"]; 19 self. age = [aDecoder decodeIntegerForKey: @ "age"]; 20 self. height = [aDecoder decodeDoubleForKey: @ "height"]; 21} 22 return self; 23} 24 @ end
If it is inherited from NSObject: you only need to change if (self = super init) in initWithCoder.
Note: 1. comply with the NSCoding Protocol and implement the two methods in the Protocol.
2. If it is inheritance, the subclass must override the two methods. This is because the subclass of person will go to the subclass to find the method to be called during access. If it is not found, it will go to the parent class to find the method, therefore, the newly added attributes are ignored during last saving and reading. You need to first call the method of the parent class, first initialize the parent class, and then initialize the Child class.
3. The suffix of the file for storing data can be named at will.
4. The data saved through plist is directly displayed, which is not safe. The data stored through the archive method is garbled in the file, making it safer.