1. Convert the data to be displayed into a model, archive the object, and then parse the stored object to use
2. Can be packaged to facilitate the use of packaging methods deposited in the Baidu Cloud, need to leave a message
/** Archive Method */
-(void) archive
{
/**
* Common Directory directory of several file directories
* Nsdownloadsdirectory Download file directory
* NSDocumentDirectory Data file directory
* Nscachesdirectory Cache file Directory
* Nsmoviesdirectory Movie file directory
* Nspicturesdirectory picture file directory
* Nsmusicdirectory Music Files directory
*
*/
Nsarray *mypaths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
The first file object in the document directory
NSString *docpath = [Mypaths firstobject];
Create a new file and return the file path
NSString *filenamepath = [DocPath stringbyappendingpathcomponent:@ "Archive.data"];
Archive
[Nskeyedarchiver archiverootobject:@ "Data Model" Tofile:filenamepath];
Read document
NSString *duixaing = [Nskeyedunarchiver Unarchiveobjectwithfile:filenamepath];
}
Another way to archive, you need to study the path of the archive, the method of archiving, the method of reading the document (annotated with two different classes, one is the archive class, one is the class of reading files)
The stored data is stored in binary form on the phone's local
/** Save data for archiving */
-(Ibaction) SAVEBTN: (ID) Sender {
NSString *filepath = [self filepathwithfilename:@ "student.archive"];
Nsmutabledata *thedata = [Nsmutabledata data];
Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:thedata];
Studentsinfo *info = [[Studentsinfo alloc] init];
Info.sno = Self.snoLabel.text;
Info.name = Self.nameLabel.text;
info.classes = Self.classLabel.text;
[Archiver encodeobject:info forkey:@ "Mystudent"];
[Archiver finishencoding];
[Thedata Writetofile:filepath Atomically:yes];
NSLog (@ "%@", FilePath);
}
/** Archive File read Data */
-(Ibaction) READBTN: (ID) Sender {
NSString *filepath = [self filepathwithfilename:@ "student.archive"];
NSData *data = [NSData Datawithcontentsoffile:filepath];
if (Data.length > 0) {
Nskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:data];
Studentsinfo *info = [Unarchiver decodeobjectforkey:@ "Mystudent"];
[Unarchiver finishdecoding];
Self.snoLabel.text = Info.sno;
Self.nameLabel.text = Info.name;
Self.classLabel.text = info.classes;
}
}
/** returns the path of a file under the document file (the file you created) */
-(NSString *) Filepathwithfilename: (NSString *) fileName
{
Nsarray *mypaths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *mydocpath = [Mypaths firstobject];
NSString *filename = [Mydocpath stringbyappendingpathcomponent:filename];
return filename;
}
Code, using methods to refer to the boss file.
iOS development data Persistence, archiving method