IOS-Archiver file archiving (2)

Source: Internet
Author: User

IOS-Archiver file archiving (2)

Archiver is a method of persistent data. The difference between Archiver and Plist is that it can persist custom objects. But it is not as convenient as Plist.

By default, data that can be persisted by Archiver includes NSNumber, NSArray, NSDictionary, NSString, and NSData.

Protocol. Suppose we want to realize the Archiver persistence of an object, we must also implement this object.


1. The protocol mainly involves two methods: archiving and file recovery.

// Restore the archive file to the object-(id) initWithCoder :( NSCoder *) aDecoder // archive, so that the object persists-(void) encodeWithCoder :( NSCoder *) acder


----------------

First, obtain the path of the archive file.

# Pragma mark file path-(NSString *) filePath {NSArray * dirPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSAllDomainsMask, YES); NSString * dirPath = dirPaths [0]; NSString * filePath = [dirPath stringByAppendingPathComponent: @ "aa. archiver "]; return filePath ;}


2. How to archive default objects (NSNumber, NSArray, NSDictionary, NSString, NSData)

# Pragma mark archive/restore Array object-(void) savearray {NSString * filePath = [self filePath]; // NSArray * arr = @ [@ "ttt ", @ "BBB", @ 25]; // [NSKeyedArchiver archiveRootObject: arr toFile: filePath]; // NSArray * arr1 = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath]; NSLog (@ "% @", arr1 );}

# Pragma mark archive/restore a Dictionary object-(void) saveDic {NSString * filePath = [self filePath]; // NSDictionary * dict ==@{ "name ": @ "lean", @ "age": @ 25}; // BOOL flag = [NSKeyedArchiver archiveRootObject: dict toFile: filePath]; // NSLog (@ "% d ", flag); NSDictionary * dict2 = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath]; NSLog (@ "% @", dict2 );}

3. How to archive custom objects. Defines a Person class as follows:

#import 
 
  @interface Person : NSObject 
  
   @property (nonatomic,copy) NSString *name;@property (nonatomic,assign) int age;+ (Person *) initWithName:(NSString *)name andAge:(int) age;@end#import "Person.h"@implementation Person+ (Person *) initWithName:(NSString *)name andAge:(int) age{    Person *p=[[Person alloc] init];    p.name=name;    p.age=age;    return p;}-(void)encodeWithCoder:(NSCoder *)aCoder{    [aCoder encodeObject:self.name forKey:@"name"];    [aCoder encodeInt:self.age forKey:@"age"];}-(id)initWithCoder:(NSCoder *)aDecoder{    [self setName:[aDecoder decodeObjectForKey:@"name"]];    [self setAge:[aDecoder decodeIntForKey:@"age"]];    return self;}@end
  
 
TIP: Both encode and decode use different methods based on the object type. For example

EncodeInt: forkey: encodeDouble: forkey: encodeFloat: forkey:

DecodeObjectForKey: decodeIntForKey: decodeDoubleForKey:


NSKeyedArchiver archiveRootObject: toFile:

NSKeyedUnarchiver unarchiveObjectWithFile:

Archive is required. Two types of operations performed on the recovered object


After defining the Person class, you can call the following method to archive the class:

# Pragma mark archive/restore custom object-(void) savePerson {NSString * filePath = [self filePath]; Person * p = [Person initWithName: @ "lean" andAge: 22]; BOOL flag = [NSKeyedArchiver archiveRootObject: p toFile: filePath]; Person * p2 = [comment unarchiveObjectWithFile: filePath]; NSLog (@ "% d-% d", flag, p2.age );}

For its Person class, assuming that there are custom objects in the class as attributes Protocol


4. Assume that this object is a subclass of an object. Here we create a class named Student as a subclass of Person.


#import "Person.h"@interface Student : Person@property (nonatomic ,assign) int no;+ (Student *) initWithName:(NSString *)name andAge:(int) age andNO:(int) no;@end

Similarly, Student also needs to implement the NSCoding protocol.

-(id)initWithCoder:(NSCoder *)aDecoder{    if (self=[super initWithCoder:aDecoder]) {        [self setNo:[aDecoder decodeIntForKey:@"no"]];    }    return self;}-(void)encodeWithCoder:(NSCoder *)aCoder{    [super encodeWithCoder:aCoder];    [aCoder encodeInt:self.no forKey:@"no"];}

# Pragma mark archive/restore a custom subclass object-(void) saveStudent {NSString * filePath = [self filePath]; Student * p = [Student initWithName: @ "lean" andAge: 22 andNO: 150133]; BOOL flag = [NSKeyedArchiver archiveRootObject: p toFile: filePath]; Student * p2 = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath]; NSLog (@ "% d-% @", flag, p2.name );}




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.