iOS development data Persistence, archiving method

Source: Internet
Author: User

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

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.