Using nskeyedarichiver for archiving,Nskeyedunarchiver , this way, the data is serialized and deserialized before the data is written and read.
1. Archiving for single strings
//Get root directoryNSString *homedictionary =nshomedirectory (); //Add a saved file nameNSString *homepath = [Homedictionary stringbyappendingpathcomponent:@"MyText.txt"]; //Archive A string---returns whether the flag is successfulBOOL flag = [Nskeyedarchiver archiverootobject:@"Archive string Contents ..."Tofile:homepath]; if(flag) {NSLog (@"Archive Successful!!!"); //to unpack a string from the archived contentNSString *str =[Nskeyedunarchiver Unarchiveobjectwithfile:homepath]; NSLog (@"%@", str); }Else{NSLog (@"Archive failed!!!"); }
/* When an array or dictionary is archived, the objects contained within them must all implement the Nscoding protocol to be able to archive successfully */
2. Archiving multiple different types of data at the same time
//a cgpoint point, String, Integer (of course many types are possible, such as uiimage, float, and so on)//Preparing DataCgpoint point = Cgpointmake (1.0,2.0); NSString*string=@"origin point of coordinates"; Nsinteger Number=Ten; NSString*multihomepath = [Nshomedirectory () stringByAppendingPathComponent:@"Yyehdh.archiver"]; Nsmutabledata*data =[[Nsmutabledata alloc]init]; Nskeyedarchiver*archvier =[[Nskeyedarchiver Alloc]initforwritingwithmutabledata:data]; //Archive Multiple Objects[Archvier encodecgpoint:point Forkey:@"Kpoint"]; [Archvier encodeobject:stringForkey:@"Kinfo"]; [Archvier encodeinteger:number Forkey:@"Kvalue"]; //Call Archive End Close function[Archvier finishencoding]; //Archive data-Returns a sign of the success of the archiveBOOL flag =[Data Writetofile:multihomepath Atomically:yes]; if(flag) {NSLog (@"SUCCESSFUL DATA archiving!!!!"); //read the data from the file you just filed.Nsmutabledata *datar =[[Nsmutabledata Alloc]initwithcontentsoffile:multihomepath]; Nskeyedunarchiver*unarchiver =[[Nskeyedunarchiver Alloc]initforreadingwithdata:datar]; //Data is resolved to get data of the corresponding data type (and the same data type when the file is archived)Cgpoint pointr = [Unarchiver decodecgpointforkey:@"Kpoint"]; NSString*infor = [Unarchiver decodeobjectforkey:@"Kinfo"]; Nsinteger valuer= [Unarchiver Decodeintegerforkey:@"Kvalue"]; //Call the end of the solution close function[Unarchiver finishdecoding]; NSLog (@"%f,%f,%@,%ld", Pointr.x,pointr.y,infor, (Long) valuer); }Else{NSLog (@"Archive failed!!!!"); }
3. Custom Object Archiving
Operation Steps
Archive:
1. Using NSData instances as archived storage data
2. Add archived content---Use key-value pairs
3. Complete the Archive
Solution Archive:
1. read files from disk, generate NSData instances
2. According to NSData instance and initial solution archive instance
3. Unlock the archive and access value based on key
////A.h//Archive Solution////Created by admin on 16/1/18.//copyright©2016 Year 123. All rights reserved.//#import<Foundation/Foundation.h>#import<UIKit/UIKit.h>@interfacea:nsobject<nscoding>-(Instancetype) Initwithname: (NSString *) name icon: (UIImage *) Animage Age: (Nsinteger) age; @property (nonatomic,copy) NSString*Name: @property (nonatomic,assign) Nsinteger age; @property (nonatomic,copy) UIImage*icon;@end
////a.m.//Archive Solution////Created by admin on 16/1/18.//copyright©2016 Year 123. All rights reserved.//#import "A.h"#defineKnamekey @ "Namekey"#defineKagekey @ "Agekey"#defineKiconkey @ "Iconkey"@implementationA-(Instancetype) Initwithname: (NSString *) name icon: (UIImage *) Animage Age: (Nsinteger) age; { if(self =[Super Init]) {Self.name=name; Self.age=Age ; Self.icon=Animage; } returnSelf ;}/*Note: To archive a custom class, each property in the class must be archived, and if it is a type that cannot be archived, we can convert it to nsvalue and then convert it to the appropriate class when it is read. */#pragmaMark-encodes the message receiver by a given archiver-(void) Encodewithcoder: (Nscoder *) acoder{[Acoder encodeobject:_name Forkey:knamekey]; [Acoder encodeinteger:_age Forkey:kagekey]; [Acoder Encodeobject:_icon Forkey:kiconkey];}#pragmaMark-Returns an initialization object from the data of a given unarchiver. - (ID) Initwithcoder: (Nscoder *) adecoder{if(self =[Super Init]) {_name=[Adecoder Decodeobjectforkey:knamekey]; _age=[Adecoder Decodeintegerforkey:kagekey]; _icon=[Adecoder Decodeobjectforkey:kiconkey]; } returnSelf ;}#pragmaMark-Returns a new instance of a copy of the message receiver. - (ID) Copywithzone: (Nszone *) zone{A*copy = [[Selfclass] [allocwithzone:zone] init]; Copy.name=[Self.name Copywithzone:zone]; Copy.age=Self.age; Copy.icon=Self.icon; returncopy;}@end
NSString *multihomepath = [Nshomedirectory () stringByAppendingPathComponent:@"Ggdh.archiver"]; A*aperson = [[A alloc]initwithname:@"Zhangsan"Icon:[uiimage imagenamed:@"Oicon"] Age: the]; NSLog (@"%@", [UIImage imagenamed:@"Oicon"]); //ArchiveNsmutabledata *data =[[Nsmutabledata alloc] init]; Nskeyedarchiver*archiver =[[Nskeyedarchiver alloc] initforwritingwithmutabledata:data]; //the Encodewithcoder of Class A will be called[Archiver Encodeobject:aperson Forkey:@"Objarchiverkey"]; [Archiver finishencoding]; //Write FileBOOL flag =[Data Writetofile:multihomepath Atomically:yes]; if(flag) {NSLog (@"The custom Object Archive was successful!!!"); NSData*data =[[Nsmutabledata alloc] initwithcontentsoffile:multihomepath]; Nskeyedunarchiver*unarchiver =[[Nskeyedunarchiver alloc] initforreadingwithdata:data]; //Get Class--the object that the document is customizedA *readperson = [Unarchiver decodeobjectforkey:@"Objarchiverkey"];//The Initwithcoder method is called[Unarchiver finishdecoding]; NSLog (@"name:%@ ~ ~ age:%ld ~ icon:%@", Readperson.name,readperson.age,readperson.icon); } Else{NSLog (@"The custom Object archive failed!!!"); }
iOS Archive solution