iOS Development UI Chapter-ios Application Data storage (XML attribute list-plist)

Source: Internet
Author: User
Tags xml attribute

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

procedure The layer in the package :(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:

#import"NJViewController.h"#import"NJPerson.h"@interface Njviewcontroller ()-(ibaction) Savedatabtnclick: (ID) sender;-(ibaction) Readdatabtnclick: (ID) sender; @end @implementation Njviewcontroller/** * Click the save button*/-(ibaction) Savedatabtnclick: (ID) Sender {//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 directoryNSString *home =nshomedirectory (); //do not recommend writing///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 ~//nsdocumentdirectory Find Documents folder//It is recommended to use the following methods to dynamically obtainNSString *doc =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; //Stitching file PathNSString *path = [Doc stringbyappendingpathcomponent:@"abc.plist"]; NSLog (@"%@", path); //Nsarray *arr = @[@ "Lnj", @ "28"]; //[arr Writetofile:path atomically:yes]; //nsdictionary *dict = @{@ "name": @ "LNJ", @ "age": @ "28"}; //call WriteToFile to write data to a file//[Dict Writetofile:path atomically:yes];        /*plist can only store some of the regular classes that the system comes with, that is, objects that have writetofile methods to save data strings/dictionaries/data/nsnumber/nsdata using plist ...*/        //Custom objects cannot be saved to plistNjperson *p =[[Njperson alloc] init]; P.name=@"LNJ"; Nsdictionary*dict = @{@" Person":@"ABC"}; [Dict Writetofile:path atomically:yes];}/** * Click the read button*/-(ibaction) Readdatabtnclick: (ID) sender {nsstring*doc =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*path = [Doc stringbyappendingpathcomponent:@"abc.plist"]    ; //reading DataNsdictionary *dict =[Nsdictionary Dictionarywithcontentsoffile:path]; NSLog (@"%@", dict);} @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)

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.