What is data persistence? Permanent storage of data
Why sit data persistence: data stored in memory, program closed, memory freed, data lost, this data is temporary
The nature of data Penggushan: data is saved as a file, stored in the program's Shahe
1. Sandbox mechanism
Each application is located in a tightly restricted section of the file system
Each application can only read files in the file system created for the program
Each application is placed in a unified folder directory within the iOS system
The essence of the sandbox is a folder, and the name is randomly assigned.
2. Location of sandboxed paths
1. Find the path relative to the program sandbox through the Finder
To find a program sandbox relative path by code
NSString *documentpath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES) [0];
NSLog (@ "%@", documentpath);
2. Sandbox composition
Document stores user data, information that needs to be backed up
Library/caches Store cache files, program-specific support files
Library/preferences Storage application's Preferences file
. App package (when IOS8, app is not stored in sandbox, there are separate folders to store all program app packages)
TMP store temporary files, such as: Download the zip package, unzip the re-delete
3. Ways to get the Sandbox directory path
Nshomedirectory--------------------> Sandbox master Path
NSDocumentDirectory--------------->document Folder
Nslibrarydirectory------------------->library Folder
Nscachesdirectory------------------>caches Folder
Nstemporarydirectory--------------->tem Folder
Code
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >//1.home home directory contains: Documents,library,tmp and an application NSLog (@ "home:%@", Nshomedirectory ()); 2.DocumentsPath path nsstring *documentspath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) [0]; NSLog (@ "documentspath:%@", Documentspath); 3.Libray NSString *libraypath = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES ) [0]; NSLog (@ "%@", Libraypath); 4.temp NSLog (@ "temp:%@", Nstemporarydirectory ()); 5.cachesPath NSString *cachespath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask , YES) [0]; NSLog (@ "cachespath:%@", Cachespath); 6.user NSString *user = Nsusername (); NSLog (@ "user:%@", user);</span>
------------------------------------------------>>>> Simple File Writing
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >//nsstring writes//1. Path NSString *documentpath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSU Serdomainmask, YES) [0]; NSLog (@ "%@", documentpath); 2. Stitching file path NSString *filepath = [documentpath stringbyappendingstring:@ "/mytext.txt"]; 3. Ready to write the content nsstring *content = @ "Hello World"; [Content Writetofile:filepath Atomically:yes encoding:nsutf8stringencoding Error:nil]; 4. Read NSString *readstring = [NSString stringwithcontentsoffile:filepath encoding:nsutf8stringencoding Error:nil]; NSLog (@ "redstring:%@", readString); Nsarray//1. Get the documents path//2. stitching file path NSString *arrayfile = [Documentpath stringbyappendingstring:@]/array.plist "]; NSLog (@ "Arraypath =%@", arrayfile); 3. Prepare content Nsarray * Contentarray = @[@ "1", @ "2", @ "3", @ "4", @ "5",]; 4. Write [Contentarray writetofile:arrayfile Atomically:yes]; 5. Read Nsarray *readarray = [Nsarray arraywithcontenTsoffile:arrayfile]; NSLog (@ "Readarray:%@", Readarray); Dictinary//1. Stitching nsstring *dictfile = [documentpath stringbyappendingstring:@ "/dict.plist"]; 2. Prepare content nsdictionary *dictcontent = @{@ "1": @ "a", @ "2": @ "B", @ "3": @ "C"}; NSLog (@ "%@", dictfile); 3. Write [dictcontent writetofile:dictfile Atomically:yes]; 4. Read dictionary nsdictionary *readdict = [Nsdictionary dictionarywithcontentsoffile:dictfile]; NSLog (@ "dict:%@", readdict);</span>
-------------------------------->>>nsfilemanager
Nsfilemanager, file management, using Detaultmanager, creating simple interest objects
You can create folders
Can create, move, copy, delete files,
You can tell if a file exists
<span style= "font-family:simhei;font-size:18px;" >//nsfilemanager//Create Folder//create a folder in documents NSString *documentspath = Nssearchpathfordirectoriesindomains (N Sdocumentdirectory, Nsuserdomainmask, YES) [0]; Create a folder in documents named "Favorites" NSString *path = [Documentspath stringbyappendingstring:@ "/personal collection"]; Create a file management (simple interest) and create a folder [[Nsfilemanager Defaultmanager]createdirectoryatpath:path Withintermediatedirectories:yes Attributes:nil Error:nil]; NSLog (@ "documentspath%@", Documentspath); Modify folder NSString *newpath = [Documentspath stringbyappendingstring:@ "/Island Culture"]; [[Nsfilemanager Defaultmanager]moveitematpath:path Topath:newpath Error:nil]; delete [[Nsfilemanager Defaultmanager]removeitematpath:documentspath Error:nil]; Determine if a file exists//return value is Bool,yes exists, no does not exist [[Nsfilemanager defaultmanager]fileexistsatpath:newpath];</span>
]
Write files--------------------------------------------------->>>> complex Objects (archive/reverse archive)
1. What is a complex object
1. Data classes that do not exist within the foundation framework
2. Cannot write to a file within a program by using the WriteToFile type method
3. Complex objects contain at least one instance object
Complex objects cannot persist data through the WriteToFile: method, only by converting complex objects to NSData, and by WriteToFile for data persistence
Transform complex objects into NSData, archive, convert NSData to complex objects, reverse archive
Complex objects write files to follow the Nscoding protocol
There are two methods, the code is as follows:
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >//called when archiving (System call)-(void) Encodewithcoder: (Nscoder *) acoder{ //Encode attributes [Acoder encodeObject:self.name Forkey:kname]; [Acoder encodeObject:self.age forkey:kage];} For anti-archive encoding-(ID) Initwithcoder: (Nscoder *) adecoder{self = [super init]; Anti-coding if (self) { self.name = [Adecoder decodeobjectforkey:kname]; } return self;} </span>
Create a person class
The archive/anti-archive code is as follows:
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >//Archive Anti-archive//Create Person class instance object person *person1 = [[Person alloc] init]; Person1.name = @ "Liu Jie"; Person1.age = @ "39"; Person *person2 = [[Person alloc] init]; Person2.name = @ "Li Shijie"; Person2.age = @ "18"; Archive used NSData Nsmutabledata *person1data = [Nsmutabledata data]; Create Archive Tool Nskeyedarchiver * archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:person1data]; Archive [archiver Encodeobject:person1 Forkey:kperson1]; [Archiver Encodeobject:person2 Forkey:kperson2]; Complete the conversion [archiver finishencoding]; Find the path NSString *docunment = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) [0]; Stitching file path NSString *personpath = [docunment stringbyappendingstring:@ "/Liu Jie. xxoo"]; Write document [Person1data Writetofile:personpath Atomically:yes]; NSLog (@ "%@", docunment); Anti-archive//through file path, get data nsdata * undata = [NSData datawithcontentsoffilE:personpath]; Anti-archiving Tool nskeyedunarchiver *unaechiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:undata]; Anti-archive Person *p1 = [Unaechiver decodeobjectforkey:kperson1]; Person *P2 = [Unaechiver decodeobjectforkey:kperson2]; End anti-archive [unaechiver finishdecoding]; NSLog (@ "name:%@", p1.name); NSLog (@ "Name:%@", P2.name);</span>
Single archive. Anti-archiving
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" > //Get Documents path nsstring *documentspath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) [0]; Stitching file path nsstring *filepath = [Documentspath stringbyappendingstring:@ "/personarray.plist"]; Instance an object person *p1 = [[Person alloc] init]; P1.name = @ "Don't make a fuss"; P1.age = @ "1"; Archive [Nskeyedarchiver archiverootobject:p1 Tofile:filepath]; Anti-archive person *p2 = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath]; NSLog (@ "Name:%@", P2.name);</span>
Multiple archives/anti-archiving
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >//Get Documents path nsstring *documentspath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) [0]; Stitching file path nsstring *filepath = [Documentspath stringbyappendingstring:@ "/personarray.plist"]; Instance an object person *pn1 = [[Person alloc] init]; Pn1.name = @ "TOM"; Pn1.age = @ "n"; Person *PN2 = [[Person alloc] init]; Pn2.name = @ "KIM"; Pn2.age = @ "+"; Nsarray *array = @[pn1,pn2]; Archive [Nskeyedarchiver Archiverootobject:array Tofile:filepath]; Anti-archive nsarray *a = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath]; NSLog (@ "%@,%@", [A[0] name],[a[1] name]);</span>
Sandbox mechanism:
A simple object is written to a file and can only be nsstring,nsarray,nsdictionary,nsdata
Complex object writes to file, complies with Nscoding protocol, implements proxy method
IOS Primary Data persistence-sandbox mechanism