iOS Development UI Chapter-ios Application Data storage (XML attribute list-plist)
One, the iOS application common data storage way
1.plist (XML attribute list archive) 2. Preferences 3.NSKeydeArchiver Archive (store custom objects) 4.sqlite3 (database, relational database, cannot directly store objects, to write some database statements, to disassemble the object storage) 5.Core Data (Object-based database, block internal links)
second, the application of sand box
Every iOS app has a? Sandbox (the application sandbox is the file system directory), which is isolated from other file systems. You must stay in your own sandbox, other apps cannot access the sandbox (hint: already open in IOS8)
The file system of the sandbox should be recorded as shown (assuming the name of the application is called layer)
Simulator should use the root path of the sandbox at: (Apple is? Username, 7.0 is emulator version)/users/apple/library/application Support/iphone simulator/7.0/applications
Three, the application of sandbox structure analysis
Package: (layer in) contains all the resource files and executable files
Documents: Save the data that needs to be persisted at run time, which is backed up when itunes synchronizes the device. For example, a game app can save a game archive in that directory
TMP: Save the temporary data that is required at run time, and then delete the corresponding file from the directory when you are finished. When the app is not running, the system may also purge files in that directory. itunes does not back up this directory when syncing the device
Library/caches: Save when the app runs? data that needs to be persisted, itunes does not back up the directory when syncing the device. Non-critical data with large storage volumes and no backup required
Library/preference: Save all of your app's preferences, iOS settings (settings) should be in this record to find the settings information. This directory is backed up when itunes syncs the device
Iv. application of sandbox common access methods
Sandbox root directory: nsstring *home = Nshomedirectory (); Documents: (2 ways)
Stitching the "Documents" string with the sandbox root
NSString *home = Nshomedirectory ();
NSString *documents = [Home stringbyappendingpathcomponent:@ "documents"]; Not recommended because a new version of the operating system may modify the directory name
Philip? Nssearchpathfordirectoriesindomains function
Nsuserdomainmask representative from the user folder to find
YES represents the wavy character "~" in the expanded path
Nsarray *array = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, NO); In iOS, only one directory matches the parameters passed in, so there is only one element in the collection
NSString *documents = [array objectatindex:0];
Tmp:nsstring *tmp = Nstemporarydirectory ();
Library/caches: (2 ways similar to documents)
Stitching the "Caches" string with the sandbox root
Philip? Nssearchpathfordirectoriesindomains function (change the function's 2nd parameter to: nscachesdirectory)
Library/preference: Access to Settings information in this directory through the Nsuserdefaults class
The corresponding code:
1 #import "NJViewController.h" 2 #import "NJPerson.h" 3 4 @interfaceNjviewcontroller ()5-(Ibaction) Savedatabtnclick: (ID) sender; 6-(Ibaction) Readdatabtnclick: (ID) sender; 7 8 @end 9 Ten @implementationNjviewcontroller One /**12 * Click the save button*/ --(Ibaction) Savedatabtnclick: (ID) Sender { the - //YouTube Practices - //nsstring *path = @ "/users/apple/library/application support/iphone simulator/7.1/applications/ A6d53e11-ddf0-4392-b2d4-fe77a96888a6/documents/abc.plist "; - + //get the application root directory -NSString *home =nshomedirectory (); + A //do not recommend writing/ at //nsstring *path = [Home stringbyappendingstring:@ "/documents"]; - //do not suggest that documents be written dead - //nsstring *path = [Home stringbyappendingpathcomponent:@ "Documents"]; - - //Nsuserdomainmask Looking in the user directory - //YES represents the user directory's ~ in //nsdocumentdirectory Find Documents folder - //It is recommended to use the following methods to dynamically obtain toNSString *doc =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; + //Stitching file Path -NSString *path = [Doc stringbyappendingpathcomponent:@"abc.plist"]; theNSLog (@"%@", path); * $ Panax Notoginseng //Nsarray *arr = @[@ "Lnj", @ " " "; - //[arr Writetofile:path atomically:yes]; the + //nsdictionary *dict = @{@ "name": @ "LNJ", @ "age": @ ""}; A //call WriteToFile to write data to a file the //[Dict Writetofile:path atomically:yes]; + - /*The plist can only store some of the regular classes that the system comes with, that is, objects with WriteToFile methods can use plist to save data 46 string/dictionary/Data/nsnumber/nsdata ... -*/ - the //Custom objects cannot be saved to plist -Njperson *p =[[Njperson alloc] init];WuyiP.name =@"LNJ"; the -Nsdictionary *dict = @{@" Person":@"ABC"}; Wu[Dict Writetofile:path atomically:yes]; - } About /**57 * Click the Read button*/ --(Ibaction) Readdatabtnclick: (ID) Sender { -NSString *doc =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; A +NSString *path = [Doc stringbyappendingpathcomponent:@"abc.plist"] the ; - //reading Data $Nsdictionary *dict =[Nsdictionary Dictionarywithcontentsoffile:path]; theNSLog (@"%@", dict); the } the @end
V. Attribute list
The attribute list is an XML-formatted file that expands to the name plist
If the object is NSString, Nsdictionary, Nsarray, NSData, NSNumber, and so on, you can use the writetofile:atomically:? method to write the object directly to the property list file
iOS Development UI Chapter-ios Application Data storage (XML attribute list-plist)