Archive only for Foundation objects:
Using an XML attribute list archive, the result is an XML file, and the attribute list archive is for the foundation object only, and if there are objects of certain custom classes in the array or dictionary collection, this method cannot be archived.
How to use the archive:
Use the method that comes with the Foundation class:-(BOOL) WriteToFile: (NSString *) path atomically: (bool) useauxiliaryfile;
The corresponding parsing method:-(ID) Initwithcontentoffile: (NSString *) FilePath;
The two methods are relative. With a self-documenting method, you must use your own method to read the file. If you read it in another way, an exception occurs.
Ways to archive Custom classes:
Filed with Nsarchiver and Nskeyedarchiver.
There is no nsarchiver in the iOS SDK (Nsarchiver is a method in the Mac OS SDK). Only Nskeyedarchiver and nskeyedunarchiver can be used for archiving and parsing operations.
Nsarray *[email protected][@ "1" @ "2" @ "3"];
[Nskeyedarchiver archiverootobject:array tofile:@ "arrtemp"]; Archive operations
Nsarray *arr=[nskeyedunarchiver unarchiveobjectwithfile:@ "arrtemp"]; Read File operation
Custom classes should follow the <NSCoding> protocol approach when archiving with Nskeyedarchiver for key-value pairs.
How to encode the archive
-(void) Encodewithcoder: (Nscoder *) Acoder
{
[Acoder encodeobject:bookname forkey:@ "name"];
}
How to read a file for decoding
-(ID) Initwithcoder: (Nscoder *) Adecoder
{
Self.bookname = [Adecoder decodeobjectforkey:@ "name"];
return self;
}
Archive Summary of iOS development