Common ways to store data in advanced UI--ios

Source: Internet
Author: User

Common ways to store data in iOS:

1,XML attribute list (plist);

2,preference preference setting ;

3nskeyedarchiver archive (nscodeing protocol )

4, SQLite 3;

5,Core Data;

......

Application sandbox:

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

Note:

By default, the emulator directory is hidden, and to display it, you need to enter the following command on the Mac Terminal:

command to display Mac hidden files: Defaults write Com.apple.finder appleshowallfiles YES

command to hide Mac hidden files: Defaults write Com.apple.finder appleshowallfiles NO

Common ways to get a sandboxed catalog:

Sandbox root: NSString *home = nshomedirectory ();D ocuments: (2 ways) splicing the "Documents" string with the sandbox root
1 nsstring *home = nshomedirectory (); 2 3 nsstring *documents = [Home stringbyappendingpathcomponent:@ "documents"];

Not recommended because a new version of the operating system may modify the directory name

Using the Nssearchpathfordirectoriesindomains function

Nsuserdomainmask representative from the user folder to find

YES represents the wavy character "~" in the expanded path

1 [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; // get the document directory 2 [Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; // Get Cache Path (caches) 3 nstemporarydirectory (); // Get temporary path (TMP) 4 nshomedirectory (); // Home Directory

In iOS, only one directory matches the parameters passed in, so there is only one element in the collection

1 nsarray *array =  nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, NO)

XML attribute list:

The attribute list is an XML-formatted file that expands to be named Plist if the object is NSString, Nsdictionary, Nsarray, NSData, NSNumber, and so on, you can use writetofile:atomically: method to write the object directly to the property list file; example code:
//get the document directoryNSString *doc =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];//Splice saved file addressNSString *plistpath = [Doc stringbyappendingpathcomponent:@"data.plist"]; Nsdictionary*data = @{@"name":@"Zhangsan",@" Age":@ at}; //write data to a plist file[Data Writetofile:plistpath Atomically:yes]; //ReadNsdictionary *dict = [Nsdictionary Dictionarywithcontentsoffile:plistpath];

Preference Preferences :

Many iOS apps support preferences such as saving usernames, passwords, font size, and so on, and iOS offers a standard set of solutions to add preferences to your app. Each app has a Nsuserdefaults instance that accesses preferences such as saving user names, font sizes, Whether to log on automatically, and so on. Example code:
//save data using preferencesNsuserdefaults*defaults =[Nsuserdefaults standarduserdefaults];//the saved data[Defaults setobject:@"Zhangsan"Forkey:@" Account"]; [Defaults setobject:@"123"Forkey:@"Password"]; [Defaults setbool:yes forkey:@"Autologin"]; [Defaults setbool:yes forkey:@"rememberpwd"]; //call the synchronous method to save the data to the sandbox file[Defaults synchronize];//reading data from user preferencesNsuserdefaults *defautls =[Nsuserdefaults Standarduserdefaults]; NSString*account = [Defautls objectforkey:@" Account"]; NSString*password = [Defautls objectforkey:@"Password"]; BOOL rememberpwd= [Defautls Boolforkey:@"rememberpwd"]; BOOL Autologin= [Defautls Boolforkey:@"Autologin"];

Note the point:

Userdefaults The data is set, it is not written immediately, but the data in the cache is written to the local disk periodically based on the timestamp. So after calling the set method, it is possible that the data has not yet been written to the disk application to terminate. The above problems can be enforced by calling the Synchornize method. nskeyedarchiver archive :If the object is NSString, Nsdictionary, Nsarray, NSData, NSNumber and other types, you can archive and restore directly with Nskeyedarchiver. Not all objects can be archived directly in this way, only objects that adhere to the Nscoding protocol. There are 2 ways to nscoding a protocol:Encodewithcoder:

This method is called every time the object is archived. In this method, you typically specify how to archive each instance variable in an object, and you can use the Encodeobject:forkey: method to archive instance variables

Initwithcoder:

This method is called every time the object is recovered (decoded) from the file. In this method, you typically specify how to decode the data in a file as an instance variable of an object, and you can use the Decodeobject:forkey method to decode the instance variable.

Example code:

1 //get the document directory2NSString *doc =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];3 4NSString *plistpath = [Doc stringbyappendingpathcomponent:@"Data.archiver"];5 6  //saving data using the "Nskeyedarchiver" method7     8Nsdictionary *data = @{@"name":@"Zhangs",@"heigt":@1.90};9     Ten //save a dictionary in the sandbox by Nskeyedarchiver One [Nskeyedarchiver archiverootobject:data Tofile:plistpath]; A  - //Read the dictionary -Nsdictionary *dict = [Nskeyedunarchiver Unarchiveobjectwithfile:plistpath];

Custom classes that adhere to the instance code of the Nscoding protocol object:

1 //2 //Person.h3 //Nskeyedunarchiver4 //5 //Created by Xiaomoge on 14/12/29.6 //Copyright (c) 2014 Xiaomoge. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten  One @interfacePerson:nsobject<nscoding> A  -@property (nonatomic,copy) NSString *name; -@property (nonatomic,assign)intAge ; the  - @end
1 //2 //person.m3 //Nskeyedunarchiver4 //5 //Created by Xiaomoge on 14/12/29.6 //Copyright (c) 2014 Xiaomoge. All rights reserved.7 //8 9 #import "Person.h"Ten  One @implementation Person A  --(void) Encodewithcoder: (Nscoder *) acoder{ -     //specify how each property is stored the[Acoder encodeObject:self.name Forkey:@"name"]; -[Acoder encodeInt:self.age Forkey:@" Age"]; - } -  +-(ID) Initwithcoder: (Nscoder *) adecoder{ -     //read Data settings per property +     if(self =[Super Init]) { ASelf.name = [Adecoder decodeobjectforkey:@"name"]; atSelf.age = [Adecoder decodeintforkey:@" Age"]; -     } -     returnSelf ; - } -  - @end

Common ways to store data in advanced UI--ios

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.