Archive NSKeyedArchiver and iosnskeyedarchiver for IOS data persistence
There are three methods for IOS Data Persistence:
The following describes an archive NSKeyedArchiver.
Archive (also known as serialization), convert the object into bytecode and store it on the disk as a file; when the program is running or when the program is re-rewritten to open the program, these objects can be restored through unarchive (deserialization.
Archive method:
- Archiving objects in the Foundation framework
- Archiving custom content
- Archiving custom objects
<1> archive objects in the Foundation framework
// Obtain the file path NSString * documentPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString * filePath = [documentPath stringByAppendingPathComponent: @ "file. archiver "]; // archive (serialization) NSArray * archiveAry = @ [@" jereh ", @" ios "]; if ([NSKeyedArchiver archiveRootObject: archiveAry toFile: filePath]) {NSLog (@ "Archiver success") ;}// unarchive (deserialization) NSArray * unArchiveAry = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath]; NSLog (@ "% @", unArchiveAry );
Summary:
<2> archiving custom content
// Obtain the file path NSString * documentPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString * filePath = [documentPath stringByAppendingPathComponent: @ "file. archiver "]; // 1. use NSData to store archive data NSMutableData * archiverData = [NSMutableData data]; // 2. create an archive object NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: archiverData]; // 3. add archive content (set key-value pairs) [archiver encodeObject: @ "jereh" forKey: @ "name"]; [archiver encodeInt: 20 forKey: @ "age"]; [archiver encodeObject: @ [@ "ios", @ "oc"] forKey: @ "language"]; // 4. archive [archiver finishEncoding]; // 5. store the archived information on the disk. if ([archiverData writeToFile: filePath atomically: YES]) {NSLog (@ "archiver success");} // unarchive // 1. read files from the disk and generate NSData instance NSData * unarchiverData = [NSData dataWithContentsOfFile: filePath]; // 2. create and initialize an archive object NSKeyedUnarchiver * unachiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: unarchiverData]; // 3. unarchive: Access NSString * name = [unachiver decodeObjectForKey: @ "name"] based on the key value; int age = [unachiver decodeIntForKey: @ "age"]; NSArray * ary = [unachiver decodeObjectForKey: @ "language"]; NSLog (@ "name = % @ age = % I ary = % @", name, age, ary );
Summary:
<3> archiving custom objects
# Define IDNUM @ "idNum" # define NAME @ "name" @ interface Student: NSObject <NSCoding> @ property (nonatomic, assign) int idNum; @ property (nonatomic, copy) NSString * name; @ end @ implementation Student # process object attributes encoded by pragma mark-(void) encodeWithCoder :( NSCoder *) acder {[acder encodeInt: _ idNum forKey: IDNUM]; [ecoder encodeObject: _ name forKey: NAME] ;}# pragma mark decodes and decodes archive data to initialize the object-(id) initWithCoder :( NSCoder *) aDecoder {if (self = [super init]) {_ idNum = [aDecoder decodeIntForKey: IDNUM]; _ name = [aDecoder decodeObjectForKey: NAME];} return self ;}@ end
Summary:
-
- The encodeWithCoder method encodes object attributes and is called during object archiving.
- The initWithCoder method decodes archive data to initialize the object. It is called when the object is archived.
Summary: