IOS Development Guide 11th Study on object archiving of data persistence

Source: Internet
Author: User
Tags date1

1 is a serialization method that first serializes the archive object into a file and then restores the data to the object through an anti-archive.

Condition: The class of the object must implement the Nscoding protocol, and each member variable should be a basic data type or an instance of a class that implements the Nscoding protocol.

Archive class Nskeyedarchiver anti-archive class Nskeyedunarchiver NSData class provides a way to read a data file

Method: +datawithcontentsoffile:

+datawithcontentsoffile:options: Specify the Read data option error:

-initwithcontentsoffile:/options:error:

-writetofile:atomically: If the file does not exist at this time create a

-writetofile:options:error:

2 Archiving: Encapsulating NSData Objects-encapsulating nskeyedarchiver objects-Encoding-completion encoding-writing files

- (void) createeditablecopyofdatabaseifneeded {Nsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; NSString*writabledbpath =[self applicationdocumentsdirectoryfile]; BOOL dbexits=[FileManager fileexistsatpath: Writabledbpath]; if(!dbexits) {NSString*path =[self applicationdocumentsdirectoryfile]; NSDateFormatter*dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Setdateformat:@"YYYY-MM-DD HH:mm:ss"]; NSDate*date1 = [Dateformatter datefromstring:@"2010-08-04 16:01:03"]; Note* Note1 =[[Note alloc] init]; Note1.date=date1; Note1.content=@"Welcome to MyNote."; Nsmutablearray* Array =[ [Nsmutablearray alloc] init]; [Array addobject:note1]; Nsmutabledata* Thedata =[Nsmutabledata data]; Nskeyedarchiver* Archiver =[[Nskeyedarchiver alloc] initforwritingwithmutabledata: thedata]; [Archiver Encodeobject:array Forkey:archive_key]; The note object inside must be the implementation nscoding protocol [archiver finishencoding
      ];    [Thedata Writetofile:path Atomically:yes]; }}-(NSString *) applicationdocumentsdirectoryfile {nsstring*documentdirectory =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*path =[Documentdirectory Stringbyappendingpathcomponent:file_name]; returnpath;}

The note class must implement the Nscoding protocol:

-(void) Encodewithcoder: (Nscoder *) Acoder {encoding [Acoder encodeobject: _date forkey:@"Date"]; [Acoder encodeobject:_content Forkey:@"content"];}-(ID) Initwithcoder: (Nscoder *) Adecoder {anti-coding self.date= [AdecoderDecodeobjectforkeY:@"Date"]; Self.content= [Adecoder Decodeobjectforkey:@"content"]; returnSelf ;}

3 Anti-Archive: query

Querying all data Methods-(nsmutablearray*) findall{nsstring *path = [self applicationdocumentsdirectoryfile];    Nsmutablearray *listdata = [[Nsmutablearray alloc] init];        NSData * thedata =[nsdata Datawithcontentsoffile:path]; If([thedata length] > 0){determine if archived objects have dataNskeyedunarchiver * archiver = [[Nskeyedunarchiver alloc]Initforreadingwithdata: Thedata]; ListData = [ArchiverDecodeobjectforkey: Archive_key];    [Archiver finishdecoding]; } return listData;}        Query data method by primary key-(note*) FindByID: (note*) model{nsstring *path = [self applicationdocumentsdirectoryfile];    Nsmutablearray *listdata = [[Nsmutablearray alloc] init];        NSData * thedata =[nsdata Datawithcontentsoffile:path];                                        if ([thedata length] > 0) {nskeyedunarchiver * archiver = [[Nskeyedunarchiver alloc]        Initforreadingwithdata:thedata];        ListData = [Archiver Decodeobjectforkey:archive_key];                [Archiver finishdecoding];                For (note* Note in ListData) {//Compare date primary key is equal if ([Note.date isEqualToDate:model.date]) {            return note; }}} return nil;}

IOS Development Guide 11th Study on object archiving of data persistence

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.