Application Sandbox structure Analysis
1. Application Package: Contains all the resource files and executable files
2. Documents: Saves data that needs to be persisted when the app runs, and it backs up the directory when itunes syncs the device
3. tmp: Save the temporary data required by the application to run, and then delete the corresponding files from the directory after use. The app is not running, the system may also clear files in that directory, itunes does not back up the directory synchronously
4. Library/cache: Saves data that needs to be persisted when the app runs, and itunes syncs the device without backing up the directory. General storage of non-essential data that is large in size and does not need to be backed up
5. Library/preference: Save all your app's preferences, and the iOS Settings app will find the app's settings in that directory. This directory is backed up when itunes syncs the device
data storage in iOS
1. Save as plist attribute list
Func Savewithfile () {/// 1, gets the root path of the sandbox let home = Nshomedirectory () as NSString; 2, obtain the documents path, using the NSString object stringByAppendingPathComponent () method stitching path Let DocPath = Home.stringbyappendingpathcomponent ("Documents") as NSString; 3, get the text file path let FilePath = Docpath.stringbyappendingpathcomponent ("Data.plist"); var dataSource = Nsmutablearray (); Datasource.addobject ("Emaciated not regret"); Datasource.addobject ("For Iraq to eliminate people gaunt"); Datasource.addobject ("The Motherland in the Moon"); Datasource.addobject ("If life is just like the beginning"); Datasource.addobject ("Look Back, the man is in the dim Place"); 4, write the data to the file datasource.writetofile (FilePath, atomically:true); println ("/(FilePath)"); }
Func Readwithfile () {/// 1, gets the root path of the sandbox let home = Nshomedirectory () as NSString; 2, obtain the documents path, using the NSString object stringByAppendingPathComponent () method stitching path Let DocPath = Home.stringbyappendingpathcomponent ("Documents") as NSString; 3, get the text file path let FilePath = Docpath.stringbyappendingpathcomponent ("data.plist"); Let DataSource = Nsarray (Contentsoffile:filepath); println ("/(DataSource)"); }
2. Using Nsuserdefaults to store data
Func savewithnsuserdefaults () {/// 1, using Nsuserdefaults to store data let defaults = Nsuserdefaults.standarduserdefaults (); 2, storage data defaults.setobject ("Emaciated end not Regret", Forkey: "name"); 3. Synchronizing Data defaults.synchronize (); }
Func readwithnsuserdefaults () {let defaults = Nsuserdefaults.standarduserdefaults (); Let name = Defaults.objectforkey ("name") as NSString; println ("/(name)"); }
3, archive storage: objects need to implement nscoding protocol, archiving corresponding encode, anti-archiving corresponding decode
/** Archiving Data requires implementation of Nscoding protocol * /func savewithnskeyedarchiver () {let home = Nshomedirectory () as NSString; Let DocPath = Home.stringbyappendingpathcomponent ("Documents") as NSString; Let FilePath = Docpath.stringbyappendingpathcomponent ("book". Data "); Let book = Cfaddressbook (name: "Francis", Call: "199"); /** * Data archive processing * /nskeyedarchiver.archiverootobject (book, tofile:filepath); }
/** Anti-archive data */ func readwithnskeyedunarchiver () {let home = Nshomedirectory () as NSString; Let DocPath = Home.stringbyappendingpathcomponent ("Documents") as NSString; Let FilePath = Docpath.stringbyappendingpathcomponent ("book". Data "); Let book = Nskeyedunarchiver.unarchiveobjectwithfile (FilePath) as Cfaddressbook; println ("/(Book.name),/(Book.call)"); }
4, SQlite3
5, CoreData
Swift's sandbox and data storage