Data storage-nskeyedarchiver and write to file introduction first describes the role of each file--how to find the location of the file--how to describe the data store: 1. Using archiving to store data 2.writetofile write methods
What information is stored in the directory under the iphone sandbox? With the path shown by the iOS program, we'll find the sandbox location for iOS, where there are three of directories
Documents: Writes all application data files to this directory. This directory is used to store user data or other information that should be backed up regularly.
Library: There are two subdirectories under this directory, caches and preferences.
Caches: For storing application-specific support files, saving the information needed during the first boot of the application.
Preferences: Saves the application's preferences file. Instead of creating a preference file directly, you should use the Nsuserdefaults class to get and set your application's preferences.
TEM: This directory is used to store temporary files, saving information that is not needed during the application restart.
How to get the file path: We can get the location of the relevant file by code:
Location of the sandbox file nshomedirectory ()// cachesdirectory location [Nssearchpathfordirectoriesindomains ( Nscachesdirectory, Nsuserdomainmask, YES) lastobject]// documentdirectory location [ Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) location of lastobject]//tmp file Location of the nstemporarydirectory ()// preferencepanesdirectory file [Nssearchpathfordirectoriesindomains ( Nspreferencepanesdirectory, Nsuserdomainmask, YES) Lastobject]
How files are stored: As far as I know how files are stored there are several ways. 1. How to archive using the Nskeyedarchiver method.
The following are described in detail in these ways:How to archive using the Nskeyedarchiver method:
NSString *filepath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject]; FilePath = [FilePath stringbyappendingpathcomponent:@ "Data.zip"]; [Nskeyedarchiver archiverootobject:[@ "Dalsfjldkjflas" datausingencoding:nsutf8stringencoding] toFile:filePath]; NSData *filedata = [[NSData alloc] init]; FileData = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath]; NSString *filestring = [[NSString alloc] Initwithdata:filedata encoding:nsutf8stringencoding]; NSLog (@ "filestirng =%@", filestring);
Key methods:
Archive Storage method for the Nskeyedarchiver class: Archiverootobject: ToFile:
Decompression method for Nskeyedunarchiver class: Unarchiveobjectwithfile
Write to file using the write to file method:
save data to file nsstring *filepath = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; FilePath = [FilePath stringbyappendingpathcomponent:@ "Stirng1.zip"]; NSLog (@ "%@", FilePath); NSString *string = @ "Write to the file method"; [String Writetofile:filepath atomically:yes encoding:nsstringencodingconversionexternalrepresentation Error:nil]; extract the appropriate data from the file nsstring *stringfile = [[NSString alloc] Initwithcontentsoffile:filepath encoding: Nsstringencodingconversionexternalrepresentation Error:nil]; NSLog (@ "%@", stringfile);
In the general data types such as:nsarray,nsmutablearray,nsdictionary,nsmutabledictionary,nsdata,nsmutabledata,nsstring,nsmutablestring, Nsnumber,nsdate, there is one method for WriteToFile. We can use this method to store the data together. Key storage method: WriteToFile Key extraction Method: Initwithcontentoffile
Data storage-nskeyedarchiver and write to file introduction