Attributes and archive of iPhone Data Storage

Source: Internet
Author: User

there are four ways to permanently store data on the iPhone: attribute list, archive, sqlite3, and coredata. The first and second operations are the same in some places. For the attribute list and archive operations, writetofile/url: path atomically: flag and initwithcontentoffile/url: path are used; neither of them can directly operate on basic data types, but the former cannot operate on custom classes, while the latter can achieve the goal through implementing the nscoding protocol. In addition, each iPhone application has three folders: Documents, TMP, and library, which are called "storage application data", "temporary data", and "Database. The data we want to save will be stored in documents. Due to the time relationship, write the text more completely.

# Import "dataprocessappdelegate. H "@ implementation dataprocessappdelegate @ synthesize window; @ synthesize dataarray;-(nsstring *) pathfileforprocess :( nsstring *) pathname {nsarray * directory = offline (nsdocumentdirectory, nsuserdomainmask, yes ); return [[directory objectatindex: 0] stringbyappendingpathcomponent: pathname];} Z-(void) writedatatofile {firstdata = [[nsstring alloc] init Withstring: @ "im first! "]; Seconddata = [[nsstring alloc] initwithstring: @" im seconddata! "]; Thirddata = [[nsstring alloc] initwithstring: @" im thirddata! "]; Nslog (@" Write: \ n first: % @ \ nscond: % @ \ nthird: % @ ", firstdata, seconddata, thirddata ); nsmutablearray * TMP = [[nsmutablearray alloc] init]; [TMP addobject: firstdata]; [TMP addobject: seconddata]; [TMP addobject: thirddata]; self. dataarray = TMP; [TMP release]; [firstdata release]; [seconddata release]; [thirddata release]; bool bwrite = [dataarray writetofile: [self pathfileforprocess: @ "mytest.txt"] atomica Lly: Yes];} // read Attribute-(void) readdatafromfile {If ([[nsfilemanager defaultmanager] fileexistsatpath: [self pathfileforprocess: @ "mytest.txt"]) {nsmutablearray * tmpread = [[nsmutablearray alloc] initwithcontentsoffile: [self pathfileforprocess: @ "mytest.txt"]; self. dataarray = tmpread; [tmpread release]; firstdata = [dataarray objectatindex: 0]; seconddata = [dataarray objectatindex: 1]; thirddata = [dataarray Objectatindex: 2]; nslog (@ "read: \ n first: % @ \ nscond: % @ \ nthird: % @", firstdata, seconddata, thirddata); return ;} nslog (@ "process firle doesnt exits! ") ;}# Pragma mark ------- object ------------- // archive write-(void) processobjectwrite {person * pobject = [[person alloc] init]; pobject. name = [[nsstring alloc] initwithstring: @ "Wenqiang"]; pobject. permission sion = [[nsstring alloc] initwithstring: @ "Project Manager"]; // [pobject setage: 24 andmarry: No]; // nsmutablearray * testdata = [[nsmutablearray alloc] init]; nsmutabledata * Data = [[nsmutabledata alloc] init]; nskeyedarc Hiver * archiver = [[nskeyedarchiver alloc] failed: Data]; [archiver encodeobject: pobject forkey: @ "kobject"]; [archiver finishencoding]; Boolean bwrite = [data writetofile: [self pathfileforprocess: @ "object2.txt"] atomically: Yes]; If (bwrite) nslog (@ "OK... "); else nslog (@" write error! "); [Archiver release]; // [pobject release];}, archive read-(void) processobjectread {nsmutabledata * Data = [nsmutabledata alloc] initwithcontentsoffile: [self pathfileforprocess: @ "object2.txt"]; nslog (@ "Data % @.. ", data); optional * unchiver = [[initalloc] initforreadingwithdata: Data]; person * TMP = [unchiver decodeobjectforkey: @" kobject "]; [unchiver finishdecoding]; nslog (@ "Object: Name: % @ wedding sion: % @ \ Nage: % @ \ n marry: % @", TMP. name, TMP. permission sion); [unchiver release]; // [TMP release]; // implementation-(void) applicationdidfinishlaunching :( uiapplication *) application {// override point for customization after application launch // [self writedatatofile]; // [self readdatafromfile]; [self processobjectwrite]; [self processobjectread]; [Window makekeyandvisible];}-(void) dealloc {[Window release]; [dataarray release]; [Super dealloc];} @ end // The following are custom classes # pragma mark ----------------------- class person ---------------- # define kname @ "keyname" # define k1_sion @ "key1_sion" # define Kage @ "keyage" # define kmarry @ "keymarry" @ implementation person @ synthesize name; @ synthesize authorization Sion; # pragma mark --------------- nscoding delegate 2 method ---------- (void) handle :( nscoder *) acder {[acder encodeobject: Name forkey: kname: kprofession]; // [ecoder encodeobject: Age forkey: Kage]; // [ecoder encodeobject: Marry forkey: kmarry];}-(ID) initwithcoder :( nscoder *) adecoder {If (Self = [Super init]) {self. name = [adecoder decodeobjectforkey: kname]; self. authorization sion = [adecoder decodeobjectforkey: kprofession]; // age = [adecoder decodeobjectforkey: Kage]; // marry = [adecoder decodeobjectforkey: kmarry];} return self ;} # pragma mark --------------- nscopying 1 method -------------- (ID) copywithzone :( nszone *) Zone {person * TMP = [[[self class] allocwithzone: Zone] init]; TMP. name = [self. name copy]; TMP. permission sion = [self. permission sion copy]; return nil;}-(void) dealloc {[Name Release]; [permission sion release]; [Super dealloc];} //-(void) setage :( nsinteger) age andmarry :( Boolean) B {// age = age; // marry = B; //} @ end

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.