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