First, the use of archiverootobject for simple archiving
Using Nskeyedarichiver for archiving, nskeyedunarchiver, this way, the data is serialized and deserialized before the data is written and read.
Archive:
- NSString *homedictionary = Nshomedirectory (); //Get root directory
- NSString *homepath = [homedictionary stringbyappendingpathcomponent:@"Atany.archiver"]; Add a saved file name
- BOOL flag = [Nskeyedarchiver archiverootobject:@ "Archive" Tofile:homepath]; //Archive A string
This way you can archive strings, numbers, and so on, as well as Nsarray and nsdictionary. The return value flag indicates whether the archive was successful, yes is successful, and no is a failure.
Pick up file:
- [Nskeyedunarchiver Unarchiveobjectwithfile:homepath]
The nskeyedunarchiver is used for the file transfer (deserialization).
There is a drawback to this way of archiving: Only one object can be archived into a file, so how do you archive multiple objects?
II. Archiving of multiple objects
The same is done with Nskeyedarchiver archive, the difference is to archive multiple objects at the same time, here we put an example of a cgpoint point, String, Integer (of course, many types can be, such as uiimage, float, etc.), Use the Encodexxx method to archive and finally write to the file by using the WriteToFile method.
Archive: Write Data
- Preparing data
- Cgpoint point = Cgpointmake (1.0, 2.0);
- NSString *info = @"Origin point of coordinates";
- Nsinteger value = 10;
- NSString *multihomepath = [Nshomedirectory () stringbyappendingpathcomponent:@"Multi.archiver"];
- Nsmutabledata *data = [[Nsmutabledata alloc]init];
- Nskeyedarchiver *archvier = [[Nskeyedarchiver alloc]initforwritingwithmutabledata:data];
- Archive Multiple Objects
- [Archvier encodecgpoint:point forkey:@"Kpoint"];
- [Archvier encodeobject:info forkey:@"Kinfo"];
- [Archvier encodeinteger:value forkey:@"Kvalue"];
- [Archvier finishencoding];
- [Data Writetofile:multihomepath Atomically:yes];
Access: Gets the data from the path construct Nskeyedunarchiver instance, using the Decodexxxforkey method to get the object in the file.
- Nsmutabledata *datar = [[Nsmutabledata Alloc]initwithcontentsoffile:multihomepath];
- Nskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc]initforreadingwithdata:dater];
- Cgpoint pointr = [unarchiver decodecgpointforkey:@"Kpoint"];
- NSString *infor = [unarchiver decodeobjectforkey:@"Kinfo"];
- Nsinteger valuer = [unarchiver decodeintegerforkey:@"Kvalue"];
- [Unarchiver finishdecoding];
- NSLog (@"%f,%f,%@,%d", Pointr.x,pointr.y,infor,valuer);
It can be seen that multiple objects are archived or very convenient, there is a problem here, where the object is the basic type of data, then how to define their own class generated instance object to archive it?
III. Archiving of Custom objects
Custom objects are widely used because they correspond to the model layer in MVC, which is the entity class. In the program, we define a lot of the entity in the model layer, such as user,teacher.
It's much more important to archive your custom objects, because in many cases we need to save the data after the home key and reload it when the program resumes, so archiving is a good choice.
First we need to customize an entity class, Archive.
Archive.h
- #import <Foundation/Foundation.h>
- @interface Archive:nsobject
- @property (copy,nonatomic) NSString *name;
- @property Nsinteger age;
- @property (copy,nonatomic) NSString *address;
- @property (copy,nonatomic) UIImage *photo;
- @end
Archive.m
- #import "Archive.h"
- #define Knamekey @ "Namekey"
- #define Kagekey @ "Agekey"
- #define Kaddress @ "Addresskey"
- #define Kphotokey @ "Photokey"
- @implementation Archive
- @synthesize name = _name;
- @synthesize age = _age;
- @synthesize address = _address;
- @synthesize photo = _photo;
- #pragma mark-nscoding
- -(void) Encodewithcoder: (Nscoder *) Acoder {
- [Acoder Encodeobject:_name Forkey:knamekey];
- [Acoder encodeinteger:_age Forkey:kagekey];
- [Acoder encodeobject:_address forkey:kaddress];
- [Acoder Encodeobject:_photo Forkey:kphotokey];
- }
- -(ID) Initwithcoder: (Nscoder *) Adecoder {
- if (self = [super init]) {
- _name = [Adecoder Decodeobjectforkey:knamekey];
- _age = [Adecoder Decodeintegerforkey:kagekey];
- _address = [Adecoder decodeobjectforkey:kaddress];
- _photo = [Adecoder Decodeobjectforkey:kphotokey];
- }
- return self;
- }
- #pragma mark-nscoping
- -(ID) Copywithzone: (Nszone *) Zone {
- Archive *copy = [[[Self class] allocwithzone:zone] init];
- Copy.name = [Self.name copywithzone:zone];
- Copy.age = Self.age;
- copy.address = [Self.address copywithzone:zone];
- Copy.photo = Self.photo;
- return copy;
- }
- @end
The Archive class has four fields (name, age, address, Avatar), except that the age is integer, and everything else is considered an object.
Note: To archive a custom class, each property in the class must be archived, and if it is a type that cannot be archived, we can convert it to nsvalue and then convert it to the appropriate class when it is read.
Archive implements three delegate Method 1) encodewithcoder:2) initwithcoder:3) Copywithzone:
1) Encodewithcoder
Encodes the receiverusing a given archiver
Encodes the message receiver by a given archiver.
The class terminal Encodewithcoder method is called when a encodeobject message is received.
2) Initwithcoder
Returns an objectinitialized from the data in a given unarchiver. (required)
Returns an initialization object from the data of a given unarchiver.
3) Copywithzone
Returnsa new instance that ' s a copy of the receiver
Returns a new instance of a copy of the message receiver.
The concept of the SDK is this, the following look at the specific code of the custom class archive, in fact, and the archive of multiple objects is the same ...
Archive:
- Save pictures and Archives
- -(Ibaction) Save: (ID) Sender {
- //Prepare data
- NSString *name = @"Xiao Yang is playing ios";
- Nsinteger age = 22;
- NSString *address = @"You guess where I am ~";
- UIImage *photo = [UIImage imagenamed:@"Loginman.jpg"];
- //Store data to class
- Archive *archivingdata = [[Archive alloc] init];
- Archivingdata.name = name;
- Archivingdata.age = age;
- archivingdata.address = address;
- Archivingdata.photo = photo;
- //Archive
- Nsmutabledata *data = [[Nsmutabledata alloc] init];
- Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:data];
- [Archiver Encodeobject:archivingdata Forkey:karchivingdatakey]; //Archivingdate's Encodewithcoder
- Method is called
- [Archiver finishencoding];
- //write file
- [Data WriteToFile:self.archivingFilePath Atomically:yes];
- }
Pick up file:
- -(Ibaction) Loadarchive: (ID) Sender {
- NSData *data = [[Nsmutabledata alloc] initWithContentsOfFile:self.archivingFilePath];
- Nskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:data];
- //Get class
- Archive *archivingdata = [Unarchiver Decodeobjectforkey:karchivingdatakey]; //Initwithcoder method is called
- [Unarchiver finishdecoding];
- //Read the data
- NSString *name = archivingdata.name;
- Nsinteger age = Archivingdata.age;
- NSString *address = archivingdata.address;
- Self.imageView.image = Archivingdata.photo;
- NSLog (@"%@| | %d| | %@ ", name,age,address);
- }
After we save, we loadarchive once.
The result is:
2013-11-04 19:29:41.743testarchives[16708:c07] Xiao Yang is playing ios| | 22| | Where do you think I am?
Get success.
Archive and Nskeyedunarchiver with Nskeyedarichiver