Data storage-nskeyedarchiver and write to file introduction

Source: Internet
Author: User
Tags tmp file

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

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.