18th-day notes (archiving and archiving operations) for IOS and 18th-day ios

Source: Internet
Author: User

18th-day notes (archiving and archiving operations) for IOS and 18th-day ios

Knowledge points of IOS Learning (oc language)

I. Archiving and archiving operations

 

1) Archiving is a process in which one or more objects are stored for future restoration, including storing objects in files and reading them later.

Archive data objects into plist files

 

2) plist files can only be stored: NSString, NSDate, NSNumber, Bool, NSData, NSArray, NSDictionary

In addition, NSArray and NSDictionary can only be of the above Type

 

3) What type of data is stored in the archive and what type of data is used to receive the data when reading the data.

 

4) Archiving cannot directly operate on data of the custom object type.

 

5) code for archiving and unarchiving instances:

1 // create a two-dimensional array (each element in the array is an array object) 2 NSMutableArray * array1 = [[NSMutableArray alloc] init]; 3 for (int I = 0; I <4; I ++) {4 [array1 addObject: [NSString stringWithFormat: @ "str % d", I + 1]; 5} 6 7 NSMutableArray * array2 = [[NSMutableArray alloc] init]; 8 for (int I = 0; I <5; I ++) {9 [array2 addObject: [NSNumber numberWithInt: arc4random () % 100]; 10} 11 12 NSArray * bigArray = @ [array1, array2]; 13 // write the array object to a file, (write to memory first. If the write is successful, save it to the file immediately.) 14 [bigArray writeToFile: @ "/Users/kingkong/Desktop/day08/array. plist "atomically: YES]; 15 16 // read the content of the plist file and save it to the array 17 NSArray * newArray = [[NSArray alloc] initWithContentsOfFile: @ "/Users/kingkong/Desktop/day08/array. plist "]; 18 NSLog (@" % @ ", newArray); 19 20 NSArray * emails = @ [@" zhangsan@163.com ", @" zhangsan@qq.com "]; 21 // create a dictionary object 22 NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys: @ "zhangsan", @ "name", @ "123456", @ "password", emails, @ "email", nil]; 23 // write the dictionary object to the file 24 [dict writeToFile: @ "/Users/kingkong/Desktop/day08/dict. plist "atomically: YES]; 25 26 // read the plist file and save it to the dictionary 27 NSDictionary * newDict = [NSDictionary dictionaryWithContentsOfFile: @ "/Users/kingkong/Desktop/day08/dict. plist "]; 28 NSLog (@" % @ ", newDict );

6) code for archiving and unarchiving Custom Data instances:

1. Define a Birthday class and follow the NSCoding protocol in the. h file, for example:

1 @ interface Birthday: NSObject <NSCoding> 2 // Date of Birth class, year, month, and day 3 @ property (nonatomic, assign) int year; 4 @ property (nonatomic, assign) int month; 5 @ property (nonatomic, assign) int day; 6 @ end


2. How to Implement the NSCoding protocol in the. m file is as follows:

1 # import "Birthday. h "2 @ implementation Birthday 3 // This method is automatically called during archiving to encode all member variables (set the corresponding key for member variables)-(void) encodeWithCoder :( NSCoder *) ACO 4 {5 [ACO encodeInt: _ year forKey: @ "year"]; 6 [ACO encodeInt: _ month forKey: @ "month"]; 7 [ecoder encodeInt: _ day forKey: @ "day"]; 8} 9 10-(id) initWithCoder :( NSCoder *) aDecoder11 {12 if (self = [super init]) {13 _ year = [aDecoder decodeIntForKey: @ "year"]; 14 _ month = [aDecoder decodeIntForKey: @ "month"]; 15 _ day = [aDecoder decodeIntForKey: @ "day"]; 16} 17 return self; 18} 19 @ end

3. How to archive and archive in the main file is as follows:

1 Birthday * B = [[Birthday alloc] init]; 2 B. year = 2015; 3 B. month = 10; 4 B. day = 25; 5 6 // B must comply with the archive Protocol 7 NSString * path = @ "/Users/kingkong/Desktop/day09/Birthday. data "; 8 // perform the archive operation 9 BOOL ret = [NSKeyedArchiver archiveRootObject: B toFile: path]; 10 if (ret) {11 // perform the archive operation 12 Birthday * b2 = [NSKeyedUnarchiver unarchiveObjectWithFile: path]; 13 NSLog (@ "year: % I", b2.year); 14}


7) archive multiple objects to a single file. instance code

1. Define a Person class to comply with the NSCoding protocol in the. h file, for example:

1 # import <Foundation/Foundation. h> 2 // If You Want To archive an object, you must comply with the archive Protocol and implement the standard method in the Protocol. 3 @ interface Person: NSObject <NSCoding> 4 @ property (nonatomic, copy) NSString * name; 5 @ property (nonatomic, assign) int age; 6-(void) print; 7 @ end


2. Implement protocol methods in. m, for example:

1 # import "Person. h "2 @ implementation Person 3 // This method is automatically called during archiving and all member variables are encoded (corresponding keys are set for member variables) 4-(void) encodeWithCoder :( NSCoder *) aCoder 5 {6 NSLog (@ "% @", NSStringFromSelector (_ cmd ));
// EncodeInt is used for integer data. encodeObject is used for strings or objects 7 [acder encodeObject: _ name forKey: @ "name"]; 8 [acder encodeInt: _ age forKey: @ "age"]; 9} 10 // This method is automatically called when the archive is Unarchived 11-(id) initWithCoder :( NSCoder *) aDecoder12 {13 // if the parent class also complies with the archive protocol, self = [super initWithCode: aDecode] 14 if (self = [super init]) {15 // Based on the encoding key value decodeIntForKey is used for integer data decodeObjectForKey for strings or objects 16 _ name = [aDecoder decodeObjectForKey: @ "name"]; 17 _ age = [aDecoder decodeIntForKey: @ "age"]; 18} 19 return self; 20} 21-(void) print22 {23 NSLog (@ "name: % @, age: % d ", _ name, _ age); 24} 25 @ end

 

3. Execute the following methods in the main file:

1 Person * p1 = [[Person alloc] init]; 2 p1.name = @ "kingkong"; 3 p1.age = 20; 4 5 NSArray * array1 = @ [@ "red ", @ "blue", @ "yellow"]; 6 7 // create an object's buffer space 8 NSMutableData * mutableData = [[NSMutableData alloc] init]; 9 // create an archive and associate the buffer of an object with 10 NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: mutableData]; 11 // encode the object and save it to the Buffer Zone 12 [archiver encodeObject: p1 forKey: @ "person"]; 13 [archiver encodeObject: array1 forKey: @ "array"]; 14 // code end 15 [archiver finishEncoding]; // 16 // write data in the buffer to the file 17 NSString * path = @ "/Users/kingkong/Desktop/day09/doc. data "; 18 BOOL ret = [mutableData writeToFile: path atomically: YES]; 19 NSLog (@" ret = % d ", ret ); 20 21 // archive Operation 22 NSData * data = [NSData dataWithContentsOfFile: path]; 23 // create a buffer for the specified data in the archive object 24 NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data]; 25 // extract data using the archive tool 26 Person * p2 = [unarchiver decodeObjectForKey: @ "person"]; 27 NSArray * array2 = [unarchiver decodeObjectForKey: @ "array"]; 28 // 29 [unarchiver finishDecoding]; 30 NSLog (@ "% @, % d", p2.name, p2.age); 31 NSLog (@ "% @", array2 );

 

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.