Sandbox is a specific folder generated for each application, the name of the folder consists of a hexadecimal data, each application's sandbox file name is not the same, is randomly generated by the system.
Sandbox home directory:
NSString *homepath = Nshomedirectory ();
There are three main directories in the Sandbox folder: 1.Documents 2.Library 3tmp
1.Documents storage is some of the more important files, but the documents placed in documents cannot be too large.
2.Library is a repository that stores some of the less important data, relatively larger, with two subfolders: caches folder, for cache files, image cache, video cache, Web cache, application cache is to clear this folder, Preferences folder, System Preferences, user settings for the application, such as: User name and password. Perferences path cannot be found by Nsuserdefaults
3.tmp store Some temporary files, such as the download of compressed package zip, decompression immediately after the removal of the compressed package.
Nssearchpathdirectory This class is used to find the file directory.
such as: nsstring *documentspath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Firstobject];
First parameter: Ask for a price name
Second parameter: Determining the search Domain
Third parameter: Determines whether it is a relative or absolute path, yes absolute NO relative
NSBundle package file//equivalent to right-click to display package contents
iOS8 before the package and sandbox are in the same directory, after IOS8, the app is stored separately in a separate file directory.
The. App files can only be read and not written (readOnly), downloaded from the AppStore is the package, the program is uploaded when the package.
Nsfilemanager file Management tool, used primarily for adding. Delete. move. Copy, etc., inherit from NSObject.
The following code example:
I. Create an image file under the caches folder
1. Get the caches file path
NSString *cachespath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) firstObject];/ /Get Caches Path First
2. Stitching Image Folder path
NSString *imagepath = [Cachespath stringbyappendingpathcomponent:@ "Imagessss"]
Stringbyappendingformat stitching on what is what;
Stringbyappendingpathextension will add one before stitching the content.
Stringbyappendingstring stitching on what is what
3. Create a File Manager
Nsfilemanager is a single case
Nsfilemanager *manager = [Nsfilemanager Defaultmanager];
4. First determine if this folder exists
BOOL isexist = [manager Fileexistsatpath:imagepath];
if (!isexist) {
does not exist
BOOL issucess = [Manager createdirectoryatpath:imagepath withintermediatedirectories:yes Attributes:nil Error:nil];
NSLog (@ "%@", issucess @ "Create succeeded": @ "Create failed");
}
deleting files
if ([manager Fileexistsatpath:imagepath]) {
BOOL issucess = [manager Removeitematpath:imagepath Error:nil];
NSLog (@ "%@", issucess @ "Delete succeeded": @ "Delete failed");
}
Two. Copy the file and copy the Plist file from the package to the image folder
1. Get the path to the plist file first, the file must be present in advance
NSString *bundlepath = [[NSBundle mainbundle] pathforresource:@ "NB" oftype:@ "plist"];
2. Get the destination path, which is the path to the image in the sandbox
NSString * Nbpath = [ImagePath stringbyappendingpathcomponent:@ "Nb.plist"];
3. Start copying
if (![ Manager Fileexistsatpath:nbpath]) {
Copy when file path is not present
BOOL issuccess = [manager Copyitematpath:bundlepath Topath:nbpath Error:nil];
NSLog (@ "%@", issuccess @ "copy succeeded": @ "copy failed");
}
three. Move the file and move the Nb.plist file under Nbpath to under Document
1. Move the previous path Nbpath
2. Get the documents path and then splice @ "Nb.plist"
NSString *topath = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) FirstObject] stringbyappendingpathcomponent:@ "Nb.plist"];
3. Mobile
BOOL issuccess = [manager Moveitematpath:nbpath Topath:topath Error:nil];
NSLog (@ "%@", Issuccess @ "mobile success": @ "Move failed");
data persistence for Nsuserdefaults
Nsuserdefaults inherit from NSObject, is also a singleton, through KVC mode to assign values, access to data when the contents of the operation, are stored in the Perference
To create a user Index object
Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];
[Defaults setinteger:100 forkey:@ "Money"];
[Defaults synchronize];//Synchronize Now, as soon as you modify the plist files in the Perferences file will be synchronized immediately.
Nsuserdefault can also manipulate other data types: Nsdictionary, Nsarray,nsstring,nsdata,nsnumber,bool,nsinteger
Nsuserdefaults generally stores some smaller data, most of which are used to store values
Impersonate the first login:
Nsuserdefaults *userdefault = [Nsuserdefaults standarduserdefaults];
BOOL IsFirst = [Userdefault boolforkey:@ "IsFirst"];
if (IsFirst = = NO) {
NSLog (@ "First time login");
[Userdefault setbool:yes forkey:@ "IsFirst"];
[Userdefault Synchronize];
}else{
NSLog (@ "Not the first time to login");
}
------------------------------------------data is written to the file-----------------------------------------
A. String is written to the file, and the string is written to the Documents folder
1. Get the document file path
NSString *docmentspath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Firstobject];
2.string.txt is the string to write to the file, stitching.
nsstring *stringpath = [Docmentspath stringbyappendingpathcomponent:@ "String.txt"];
3. Prepare the string to be written
NSString *string = @ "Ma Zhen and Uniqlo which one do you choose?";
4. Write
Nserror *error = nil;
BOOL issuccess = [string Writetofile:stringpath atomically:yes encoding:nsutf8stringencoding error:&error];
NSLog (@ "%@", issuccess @ "string Write succeeded": @ "string write Failed");
First parameter: The file path to be written, if this file is not in the file path, a file is created automatically
Second parameter: Determine if auxiliary files need to be generated, in order to protect security under multithreading.
Third parameter: encoding format.
Fourth parameter: Error
String value operation
NSString *getstring = [[NSString alloc]initwithcontentsoffile:stringpath encoding:nsutf8stringencoding Error:nil];
NSLog (@ "%@", getString);
Two. Write the array, write to the Array.txtzhong under the library path
1. Get the Library path
NSString *libraraypath = [Nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES) firstObject ];
2. Stitching the array.txt into the Librarypathhou
NSString *arraypath = [Libraraypath stringbyappendingpathcomponent:@ "Array.txt"];
3. Preparing an array for writing to a file
Nsarray *array = @[@ "dog", @ "small fish", @ "Mimi", @ "Shanbao"];
4. Write
BOOL issuccess = [array Writetofile:arraypath atomically:yes];
NSLog (@ "%@", issuccess @ "Array write succeeded": @ "Array write failed");
Array Read
Nsarray *getarray = [Nsarray Arraywithcontentsoffile:arraypath];
NSLog (@ "%@", GetArray);
Three. The dictionary is written to the file and written to the dictionary.txt under caches.
1. Get the caches path now
NSString *cachespath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) firstObject];
2. Stitching
NSString * Dictionarypath = [Cachespath stringbyappendingpathcomponent:@ "Dictionary.txt"];
3. Prepare a dictionary for writing
Nsdictionary *dictionary = @{@ "a": @ "123", @ "B": @ "456", @ "C": @ "789"};
4. Write
BOOL issuccess = [dictionary Writetofile:dictionarypath atomically:yes];
NSLog (@ "%@", issuccess @ "Dictionary write succeeded": @ "dictionary write Failed");
Read
Nsdictionary *getdictionary = [Nsdictionary Dictionarywithcontentsoffile:dictionarypath];
NSLog (@ "%@", getdictionary);
-----------------------------------------------NSData (binary stream) file write------------------------------
Data.txt written under TEM
1. Get the road strength of the TEM
NSString *tmppath = Nstemporarydirectory ();
2.data.txt Stitching to Tmppath
NSString *datapath = [Tmppath stringbyappendingpathcomponent:@ "Data.txt"];
3. Preparing the NSData object for writing
NSString *string = @ "Uncle, let's have a date!";
NSData *data = [string datausingencoding:nsutf8stringencoding];//strings converted to NSData
4. Write
BOOL issuccess = [data Writetofile:datapath atomically:yes];
NSLog (@ "%@", issuccess @ "Data write succeeded": @ "Data write Failed");
Data from the file to take the value
NSData *getdata = [NSData Datawithcontentsoffile:datapath];
NSLog (@ "%@", getData);
NSString *getstring = [[NSString alloc]initwithdata:getdata encoding:nsutf8stringencoding];
NSLog (@ "%@", getString);
----------------------------------------------archiving and anti-archiving of complex objects-----------------------------------
A complex object cannot write to a file, and to write a complex object to a file, it must be converted to data (a binary stream) to be written.
Archive: The essence of archiving is to convert other types of data first to NSData and then to files. Here is an example of a person as a complex object.
Complex objects that are capable of archiving and anti-archiving must comply with the Nscoding protocol.
I. Archiving
1. Create a complex object person
Person *person = [Person alloc]initwithname:@ "Rose" gender:@ "Girl" age:18];
2. Create a variable data storage compressed
Nskeyedarchiver compression tool, inherited from Nscoder, mainly used for encoding
Nsmutabledata *data= [nsmutabledata data];
Nskeyedarchiver *archiver =[[nskeyedarchiver Alloc]initforwritingwithmutabledata:data];
3. Compress the person object to data using the compression tool
[Archiver encodeobject:person forkey:@ "person"];
4. Complete the compression, need to stop the compression tool
[Archiver finishencoding];
5. Write the compressed file to the Person.txt file
NSString *homepath = Nshomedirectory ();
NSString *personpath = [HomePath stringbyappendingpathcomponent:@ "Perosn.txt"];
BOOL issuccess = [data Writetofile:personpath atomically:yes];
NSLog (@ "%@", issuccess @ "person writes successfully": @ "person writes failed");
Two. Anti-archiving
The reading of a complex object is called an anti-archive
Nskeyedunarchiver Anti-archiving tool
1. Get the data in the file, a. File path B. Find a data object to receive read results
NSData *undata = [NSData Datawithcontentsoffile:personpath];
2. Creating an anti-archiving tool
Nskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc]initforreadingwithdata:undata];
3. Unzip
Person *unperson = [unarchiver decodeobjectforkey:@ ' person '];
4. Stop decompression
[Unarchiver finishdecoding];
NSLog (@ "%@", Unperson);
Archiving and anti-archiving of ~~~~~~ arrays ~~~~~~~~
If a collection wants to be archived and archived, the elements inside it must also follow the Nscoding protocol.
Person *person1 = [Person alloc]initwithname:@ "Fan Bingbing" gender:@ "male" age:30];
Person *person2 = [Person alloc]initwithname:@ "big black Bull" gender:@ "female" age:28];
Nsarray *array = @[person1,person2];
Create compressed data
Nsmutabledata *data = [Nsmutabledata data];
Creation of archiving tools
Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc]initforwritingwithmutabledata:data];
Compressing an archived object
[Archiver encodeobject:array forkey:@ "Array"];
Compression end
[Archiver finishencoding];
Store the file in the TMP directory, array.txt
NSString *tmppath = Nstemporarydirectory ();
NSString *arraypath = [Tmppath stringbyappendingpathcomponent:@ "Array.txt"];
BOOL issuccess = [data Writetofile:arraypath atomically:yes];
NSLog (@ "%@", issuccess @ "Array write succeeded": @ "Array write failed");
Initializing Undata via file path
NSData *undata = [NSData Datawithcontentsoffile:arraypath];
Create an anti-archive object
Nskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc]initforreadingwithdata:undata];
Extracting data
Nsarray *unarray = [unarchiver decodeobjectforkey:@ "Array"];
Stop the anti-archiving tool
[Unarchiver finishdecoding];
NSLog (@ "%@%@", unarray[0],unarray[1]);
UI Basics: Datapersistent. Sandbox