Archive NSKeyedArchiver and iosnskeyedarchiver for IOS data persistence

Source: Internet
Author: User

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:

 

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.