Ios-archiver file Archive (2)

Source: Internet
Author: User

Archiver is a way of persisting data, and the difference between him and plist is that he can persist his own definition of objects. But he is not plist so convenient.

Archiver data persisted by default is Nsnumber,nsarray,nsdictionary,nsstring,nsdata, because these objects have been implemented

<NSCoding> agreement. If we want to implement the archiver persistence of an object, we must also implement the object.


1.<NSCoding> protocol mainly for archiving/recovery of files two methods

Recover archive File As Object-(ID) Initwithcoder: (Nscoder *) adecoder//archive, make object persistent-(void) Encodewithcoder: (Nscoder *) Acoder


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

For example, the following. We first get the path to the archive file

#pragma mark gets the file path-(NSString *) filepath{    Nsarray *dirpaths=nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsalldomainsmask, YES);    NSString *dirpath=dirpaths[0];    NSString *filepath=[dirpath stringbyappendingpathcomponent:@ "Aa.archiver"];    return FilePath;}


2. How the system default objects are archived (nsnumber,nsarray,nsdictionary, Nsstring,nsdata)

#pragma mark archive/Restore Array object-(void) savearray{        nsstring *filepath=[self filepath];////    nsarray *[email protected ][@ "TTT", @ "BBB", @25];//    [nskeyedarchiver archiverootobject:arr tofile:filepath];//    nsarray *arr1=[ Nskeyedunarchiver Unarchiveobjectwithfile:filepath];    NSLog (@ "%@", arr1);}

#pragma mark archive/Restore Dictionary objects-(void) savedic{    nsstring *filepath=[self filepath];//nsdictionary *[email    protected]{@ "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 your own definition object.

Defines a person class. For example, the following:

#import <Foundation/Foundation.h> @interface person:nsobject <NSCoding> @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 are based on the type of object used to choose different methods. As

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

DecodeObjectForKey:decodeIntForKey:decodeDoubleForKey:


Nskeyedarchiver Archiverootobject:tofile:

Nskeyedunarchiver Unarchiveobjectwithfile:

Each is on the need to file.

Two classes to manipulate the recovered objects


Once you have defined the person class, call the following for example where you need to archive:

#pragma mark archives/restores its own definition object-(void) saveperson{    nsstring *filepath=[self FilePath];    Person *p=[person initwithname:@ "lean" andage:22];    BOOL flag=[nskeyedarchiver archiverootobject:p Tofile:filepath];    Person *p2=[nskeyedunarchiver Unarchiveobjectwithfile:filepath];    NSLog (@ "%d-%d", Flag,p2.age);}

for its person class, if the class also has its own definition object as a property. Same implementation <NSCoding> Protocol


4. If the object is a subclass of an object, here we create a subclass called the student class as the person


#import "Person.h" @interface student:person@property (nonatomic, assign) int no;+ (Student *) Initwithname: (NSString *) n Ame andage: (int) Age Andno: (int) No; @end

The same student also need to implement the Nscoding protocol method

-(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 archives/restores its own 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);}




Ios-archiver file Archive (2)

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.