Click here to download iOS archive, persist storage, extract archive detailed project
The nature of data persistence: reading data into files is saved locally. Sandbox mechanism is the system for each program in the local generated folder (name randomly generated), for different applications, can not access the contents of other application sandbox, for the application content protection: 1 Documents: Used to store long-lasting data 2 Xxx.app: The application's package, which contains all the resources required for the application to load (ReadOnly read-only, non-modifiable), NSBundle is the package 3 library:1) Caches: Local cache, storing data that you want to temporarily save (Videos, musics, Images) such as: Downloaded video, audio, images are stored in this folder 2) Preferences: Store user preferences, such as whether the program is the first time to start 4 tmp: The store has not downloaded the video, audio, when downloaded, the file is transferred to the caches folder #im Port "WYLReadAndWriteViewController.h" #import "WYLArchive.h" @interface Wylreadandwriteviewcontroller () < Uitextfielddelegate> @end @implementation wylreadandwriteviewcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} -(void) viewdidload{[Super Viewdidload]; Do any additional setup after loading the view. Self.view.backgroundColor = [Uicolor Whitecolor]; Self.navigationItem.title = @ "file read and write"; Uitextfield *textfield = [[UITExtfield Alloc]initwithframe:cgrectmake (40, 84, 220, 40)]; Textfield.tag = 100; Textfield.placeholder = @ "Please enter content"; Textfield.delegate = self; Textfield.borderstyle = Uitextborderstyleroundedrect; [Self.view Addsubview:textfield]; [TextField release]; Uitextfield *textfield2 = [[Uitextfield alloc]initwithframe:cgrectmake (40, 174, 220, 40)]; Textfield2.tag = 101; Textfield2.placeholder = @ "Show the contents of the previous input box"; Textfield2.delegate = self; Textfield2.borderstyle = Uitextborderstyleroundedrect; [Self.view Addsubview:textfield2]; [TextField2 release]; UIButton *writebutton = [UIButton Buttonwithtype:uibuttontypesystem]; Writebutton.frame = CGRectMake (45, 260, 60, 30); [Writebutton settitle:@ "write" forstate:uicontrolstatenormal]; [Writebutton addtarget:self Action: @selector (write:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:writebutton]; UIButton *readbutton = [UIButton Buttonwithtype:uibuttontypesystem]; ReadbuttOn.frame = CGRectMake (190, 260, 60, 30); [Readbutton settitle:@ "read" forstate:uicontrolstatenormal]; [Readbutton addtarget:self Action: @selector (read:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:readbutton]; UIButton *push = [UIButton Buttonwithtype:uibuttontypesystem]; Push.frame = CGRectMake (120, 310, 60, 30); [Push settitle:@ "push" forstate:uicontrolstatenormal]; [Push addtarget:self Action: @selector (push:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:push]; }-(NSString *) getfilepath{//used to get the path of the specified folder:< #NSSearchPathDirectory directory#>: Specified folder; <# Nssearchpathdomainmask Domainmask#>: Set the lookup domain, our own files are stored in the;< domain #BOOL expandtilde#>: Whether to use the detailed path (absolute path) Because the method was originally used with the Mac OS, and for the computer system, may be stored multiple users, so get to the user may have more than one, so the return value type is an array, but for iOS, there will be only one user, so there is only one element in the array/* NSString *d Ocumentspath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) firstObject]; 2) stitching on the file to be storedPath NSString *newfilepath = [Documentspath stringbyappendingpathcomponent:@ "Aa.txt"]; NSLog (@ "%@", Newfilepath); */NSString *filepath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) FirstObject] ; NSString *newpath = [FilePath stringbyappendingpathcomponent:@ "test.txt"]; NSLog (@ "%@", NewPath); return NewPath;} -(void) READ: (UIButton *) button{//Each write will overwrite the previous content, if you want to retain the previous data, you need to read the previous data, and then the data will be stored together, together with/* NSString *NEWF Ilepath = [self getfilepath]; Nserror *error = nil; NSString *content = [NSString stringwithcontentsoffile:newfilepath encoding:nsutf8stringencoding error:&error]; Uitextfield *TF = (Uitextfield *) [Self.view viewwithtag:101]; Tf.text = content; *///string read from local/* NSString *filepath = [self getfilepath]; NSString *content = [NSString stringwithcontentsoffile:filepath encoding:nsutf8stringencoding Error:nil]; Uitextfield *TF = (Uitextfield *) [Self.view viewwithtag:101]; Tf. Text = content; *//array read from local file nsstring *filepath = [self getfilepath];//nsarray *arr = [Nsarray arraywithcontentsoffile:filepat h]; Read from the dictionary locally nsdictionary *dic = [Nsdictionary Dictionarywithcontentsoffile:filepath]; Uitextfield *TF = (Uitextfield *) [Self.view viewwithtag:100]; Uitextfield *TF1 = (Uitextfield *) [Self.view viewwithtag:101]; Tf.text = dic[@ "TF2"]; Tf1.text = dic[@ "TF1"]; }//file reads and writes are only supported temporarily: NSString, Nsarray, Nsdictionary, NSData, and their subclasses. Write File: WriteToFile: (This is the method called by the object), read the file: Each class comes with a way to create an object based on a path: [Class name Class Withcontentsoffile]; string: [NSString Stringwithcontentsoffile], array: [Nsarray arraywithcontentsoffile], dictionary: [nsdictionary Dictionarywithcontentsoffile], binary stream: [NSData datawithcontentsoffile], (Keep in mind: for arrays, dictionaries such as container classes, internal members must also be able to implement the file read and write one of the eight categories) -(void) write: (UIButton *) button{//write, writes the text in the first input box to the local file//1 gets the contents of the store Uitextfield *TF = (Uitextfield *) [self. View VIEWWITHTAG:100]; NSString *content = Tf.text; 2 get to the file path you want to store//1) Get Documents folder path NSString *newfilepath = [self getfilepath]; 3 Store content to the specified file path//Nserror *error = nil; String written to local file/BOOL issucceed = [content writetofile:newfilepath atomically:yes encoding:nsutf8stringencoding error:&am P;error]; The array is written to the local file Uitextfield *TF2 = (Uitextfield *) [Self.view viewwithtag:101]; NSString *content1 = tf2.text;//Nsarray *arr = @[content, content1];//BOOL issucceed = [arr writetofile:newfilepat H Atomically:yes]; Dictionary written to local file nsdictionary *dic = @{@ "TF1": Content, @ "TF2": Content1}; BOOL issucceed = [dic writetofile:newfilepath Atomically:yes]; NSLog (@ "%d", issucceed); }-(void) Push: (UIButton *) button{wylarchive *ARCHIVIEVC = [[Wylarchive alloc]init]; [Self.navigationcontroller PUSHVIEWCONTROLLER:ARCHIVIEVC animated:yes];} -(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{[TextField Resignfirstresponder]; return YES;} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources thAt can is recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{//Get The new view controller using [Segu E Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end #import "WYLArchive.h" #import "Person.h" @interface wylarchive () <uitextfielddelegate>@ End@implementation wylarchive-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} -(void) viewdidload{[Super Viewdidload]; Do any additional setup after loading the view. Self.view.backgroundColor = [Uicolor Whitecolor]; Self.navigationItem.title = @ "Archive and reverse archive"; Uitextfield *textfield = [[Uitextfield alloc]initwithframe:cgrectmake (40, 84, 220, 40)]; TextField.tag = 100; Textfield.placeholder = @ "Please enter content"; Textfield.borderstyle = Uitextborderstyleroundedrect; Textfield.delegate = self; [Self.view Addsubview:textfield]; [TextField release]; Uitextfield *textfield2 = [[Uitextfield alloc]initwithframe:cgrectmake (40, 174, 220, 40)]; Textfield2.tag = 101; Textfield2.placeholder = @ "Show the contents of the previous input box"; Textfield2.borderstyle = Uitextborderstyleroundedrect; Textfield2.delegate = self; [Self.view Addsubview:textfield2]; [TextField2 release]; UIButton *filebutton = [UIButton Buttonwithtype:uibuttontypesystem]; Filebutton.frame = CGRectMake (45, 260, 60, 30); [Filebutton settitle:@ "Archive" forstate:uicontrolstatenormal]; [Filebutton addtarget:self Action: @selector (file:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:filebutton]; UIButton *archivebutton = [UIButton Buttonwithtype:uibuttontypesystem]; Archivebutton.frame = CGRectMake (190, 260, 60, 30); [Archivebutton settitle:@ "anti-archiving[Forstate:uicontrolstatenormal]; [Archivebutton addtarget:self Action: @selector (archive:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:archivebutton];} -(NSString *) getpath{//Get the path to the folder/* NSString *filepath = [Nssearchpathfordirectoriesindomains (nsdocumentdirector Y, Nsuserdomainmask, YES) firstobject]; NSString *newpath = [FilePath stringbyappendingpathcomponent:@ "Archive"]; return NewPath; */NSString *path = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) firstObject]; NSString *newpath = [path stringbyappendingpathcomponent:@ "Archiver"]; return NewPath;} -(void) file: (UIButton *) button{//Get the contents of the input box Uitextfield *TF1 = (Uitextfield *) [Self.view viewwithtag:100]; Uitextfield *TF2 = (Uitextfield *) [Self.view viewwithtag:101]; /*//Package Person Object *person = [[Person alloc] InitWithName:tf1.text gender:tf2.text age:18]; 1 Creating an Archive object Nsmutabledata *data = [Nsmutabledata data]; Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:data]; 2 Archive [archiver encodeobject:person forkey:@ "person"]; [Person release]; 3 End Archive, when archiving is finished, the archive is invalid [archiver finishencoding]; [Archiver release]; 4 data written to file [data writetofile:[self GetPath] atomically:yes]; */person *person = [[Person alloc] InitWithName:tf1.text gender:tf2.text age:18]; Nsmutabledata *data = [Nsmutabledata data]; Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:data]; [Archiver encodeobject:person forkey:@ "Archiver"]; [Person release]; [Archiver finishencoding]; [Archiver release]; [Data writetofile:[self GetPath] atomically:yes];} -(void) Archive: (UIButton *) button{/*//1 Initialize Nsmutabledata object Nsmutabledata *data = [Nsmutabledata datawithconten Tsoffile:[self GetPath]]; 2 Create an anti-archive object Nskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:data]; //3 Anti-archive person *person = [unarchiver decodeobjectforkey:@ ' person ']; 4 End anti-archive [unarchiver finishdecoding]; [Unarchiver release]; */Nsmutabledata *data = [Nsmutabledata datawithcontentsoffile:[self GetPath]; Nskeyedunarchiver *unarchive = [[Nskeyedunarchiver alloc] initforreadingwithdata:data]; Person *person = [unarchive decodeobjectforkey:@ "Archiver"]; [Unarchive finishdecoding]; [Unarchive release]; Uitextfield *TF1 = (Uitextfield *) [Self.view viewwithtag:100]; Uitextfield *TF2 = (Uitextfield *) [Self.view viewwithtag:101]; Tf1.text = Person.gender; Tf2.text = Person.name;} -(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{[TextField Resignfirstresponder]; return YES;} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) preparefOrsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{//Get The new view controller using [Segue Destinationviewcontro Ller]. Pass the selected object to the new view Controller.} */@end
Archive and unpack iOS, persist storage