How iOS data is stored

Source: Internet
Author: User

There are 4 of data storage methods commonly used in 1,ios

1 "XML attribute list (plist) archive

2 "Preference (preference setting)

3 "Nskeyedarchiver Archive

4 "SQLite

2, each application has its own sandbox, and the path of Shahe is

/users/ Apple /library/application Support/iphone simulator/ 7.0 /applications (Apple is user name, 7-bit emulator version)

Each application of Shahe is independent, the structure of the main there;

Document: This is used primarily to save data that is generated by the runtime "user" that needs to be persisted, which is backed up when itunes synchronizes. (General application Download data do not save in this directory, or Apple audit when the application pass, I have eaten this loss.) )

TMP: Save app Run is required temporary data, after use, and then delete, itunes Sync will not back up the directory.

Library:

1 library/caches: Saves data that needs to be persisted when the app runs, and itunes does not back up the directory when it syncs.

2 "Library/preference: Save the app's preferences, itunes will back up the directory when syncing.

3, the following gives you a few simple case procedures, caught dead.

if the list of attributes is XML format, expand the file name to plist

? if the object is NSString , nsdictionary , Nsarray , NSData , NSNumber and other types, you can use the writetofile:atomically: The method directly writes the object to the list of attributes? file.

1>: Document:

Write Data:

{

//1, get the path to the Document in the sandbox

nsstring *documents = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, YES) lastobject];

//2, stitching the path to the saved file

nsstring *path = [Documents stringbyappendingpathcomponent:@ "Array.plist" ];

//3, write the data into the plist.

nsarray *array = @[@ "Li",@ "Kui",@ "Liang"]< /c24>;

[Array writetofile:p ath atomically:YES];

}

Read data:

{

, stitching the path to the saved file

nsstring *documents = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, YES) lastobject];

nsstring *path = [Documents stringbyappendingpathcomponent:@ "Array.plist" c12>];

nsarray *array = [nsarrayarraywithcontentsoffile:p Ath];

NSLog (@ "array =%@", array);

}

2 >: Preference setting Preferences:

Cons: (1) cannot save a custom object

(2) How much data is stored in the same file without any

Save data:

{

// Gets the preference object, which saves the data to perferences by default , and has a default file name, so you don't have to specify the file name and path

// General standard,default, starting with single-instance objects

nsuserdefaults *defaults = [nsuserdefaultsstandarduserdefaults];

// Save Data

[Defaults setobject:@ "LKL" forkey:@ "name"];

[Defaults setinteger: forkey:@ "age"];

[Defaults setdouble:1.75 forkey:@ "Heigth"];

// The data in general Perferences is not saved immediately and needs to be called immediately by this method to save

[Defaults synchronize];

}

Read data:

{

nsuserdefaults *defaults = [nsuserdefaultsstandarduserdefaults];

nsstring *name = [Defaults stringforkey:@ "name"];

double heigth = [Defaults doubleforkey:@ "Heigth"];

Nsinteger age = [Defaults integerforkey:@ ' age '];

}

Nskeyedarchiver:

? If you are a custom object, you cannot call Writetofile:atomically:? method to write the object directly to the list of attributes? in the file.

    • if the object is NSString , nsdictionary , Nsarray , NSData , NSNumber type, can be used directly? Nskeyedarchiver in-line archiving and recovery

    • not all objects can be directly used in such a way ? nscoding the object of the agreement can be

    • nscoding Agreement has 2 ? Method:

Encodewithcoder :

each time the object is archived, it will be tuned to use this method. In this way? Specifies how to archive each instance variable in an object, so that you can use the encodeobject:forkey:? method to archive instance variables

Initwithcoder :

every time you recover from a file. ( decoding ) object, it will be adjusted to use this method. Usually in this? How do I decode? The data in the file is an instance variable of an object, which can be used todecode instance variables with the Decodeobject:forkeymethod.

Examples:

nskeyedarchiver- Archive Person object (Person.h) @interface person:nsobject< nscoding >

@property (nonatomic, copy) NSString *name; @property (nonatomic, assign) int age; @property (nonatomic, assign) float Heigh t; @end

@implementation person

  • -(void) Encodewithcoder: (Nscoder *) Encoder {

    [encoder encodeobject: Self.name forkey:@ "name"];

  • [encoder encodeint: Self.age forkey:@ "age"];

  • [encoder encodefloat: self.height forkey:@ "height"];

    }

  • -(ID) Initwithcoder: (Nscoder *) Decoder {

  • self.name = [decoder decodeobjectforkey: @ "name"];

  • self.age = [decoder decodeintforkey: @ "age"];

  • self.height = [decoder decodefloatforkey: @ "height"];

  • return self;

    }

  • ? Archive ( code )
    person *person = [[[Person alloc] init] autorelease]; person.name = @ "LKL";
    Person.age = 25;
    Person.height = 1.75f;
    [Nskeyedarchiver Archiverootobject:person Tofile:path];

    ? Recovery ( decoding )

    Person *person = [Nskeyedunarchiver Unarchiveobjectwithfile:path];

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.