Overview of iOS development sandbox storage

Source: Internet
Author: User

1.Property Listplist Storage

    /*

     plist documents directory and can only be the system comes with some general class , writetofile plist Save data

string / dictionary / data /nsnumber/nsdata ...

Custom objects cannot be saved to plist

*/


/** * Click on save button */-(ibaction) Savedatabtnclick: (ID) Sender {//nsuserdomainmask in User directory Lookup//YES for expand User Directory ~ NSDocumentDirectory Find Documents folder NSString *doc = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUse    Rdomainmask, YES) lastobject];    Stitching file path above gets the file path plus file name NSString *path = [Doc stringbyappendingpathcomponent:@ "Abc.plist"];    NSLog (@ "%@", Path); /* 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 strings/dictionaries/Data/nsnumber/nsdata ... */////Custom objects not    Can be saved to plist nsdictionary *dict = @{@ "person": @ "abc"}; [Dict Writetofile:path atomically:yes];} /** * Click the Read button */-(ibaction) Readdatabtnclick: (ID) Sender {nsstring *doc = [Nssearchpathfordirectoriesindomains (NSDocum        Entdirectory, Nsuserdomainmask, YES) lastobject];    NSString *path = [Doc stringbyappendingpathcomponent:@ "Abc.plist"];    Read data nsdictionary *dict = [Nsdictionary Dictionarywithcontentsoffile:path]; NSLog (@ "%@", Dict);}
Property List - Storage and reading procedures for Nsdictionary


2. Preference Settings

Preferences in preference in the library directory

<pre name= "code" class= "OBJC" >-(ibaction) SAVEBTN: (ID) Sender {//preferences are specifically used to save configuration information for the application, such as saving user name, password, font size and other settings, In general, do not save other data in preferences//If you use System Preferences to store data, the default is to store the Preferences folder under the//preferences will save all the data in the same file//get Nsuserdefaults        Like Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];    Save the data (if no synchronization occurs after setting the data, the data is automatically saved to the Preferences folder at a later point in time) [Defaults setobject:@ "Tinghou" forkey:@ "name"];    [Defaults setobject:@ "Man" forkey:@ "gender"];    [Defaults setinteger:13 forkey:@ "age"];            [Defaults setdouble:10.1 forkey:@ "height"]; Let Nsuserdefaults immediately save the data note: When setting the data, Userdefaults does not write immediately, but instead writes the cached data to the local disk according to 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 problem can be forced to write [defaults synchronize] by calling the Synchornize method; If you want to save immediately you need this line of code}-(ibaction) READBTN: (ID) Sender {//1. Get nsuserdefaults nsuserdefaults *defaults = [Nsuserdefaul    TS Standarduserdefaults];    2. Obtain the saved data via nsuserdefaults nsstring *name = [Defaults objectforkey:@ "name"]; int age = [Defaults integerforkey:@ "Age"];    NSLog (@ "%@", name); NSLog (@ "%d", age);}


+ (BOOL) isfirstopen{    nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];    BOOL Firstopen = [userdefaults boolforkey:@ "Firstopen"];    return firstopen;} + (void) Setfirstopen: (BOOL) b{    nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];    [Userdefaults setbool:b forkey:@ "Firstopen"];    [Userdefaults synchronize];} + (BOOL) login{    nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];    BOOL loginstate = [userdefaults boolforkey:@ "Login"];    return loginstate;} + (BOOL) Setlogin: (bool) loginstate{    nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];    [Userdefaults setbool:loginstate forkey:@ "Login"];    [Userdefaults synchronize];    return loginstate;}


3. Archiveif the object isNSString,nsdictionary,Nsarray,NSData,NSNumberand other types can be used directlyNskeyedarchiverfor archiving and recoverynot all objects can be archived directly in this way, only by adhering to thenscodingthe object of the agreement can benscodingAgreement has2a method: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:

every time that you recover from a file ( decoding ) object, this method is called. 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

-(Ibaction) Savebtnclick: (ID) Sender {//1. Create Object/* Njperson *p = [[Njperson alloc] init];    P.name = @ "LNJ";    P.age = 100;     P.height = 1.76;    */Njstudent *stu = [[Njstudent alloc] init];    Stu.name = @ "LNJ";    Stu.age = 100;    Stu.height = 1.8;        Stu.weight = 60; 2. Get the file path (get file path method same as plist storage) nsstring *docpath = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuse    Rdomainmask, YES) lastobject];    Using the file path (document) obtained above plus the file name stitching files path nsstring *path = [DocPath stringbyappendingpathcomponent:@ "Stu.xxoo"];        NSLog (@ "path =%@", path);    3. Save the custom object to a file//[Nskeyedarchiver archiverootobject:p Tofile:path];    [Nskeyedarchiver Archiverootobject:stu Tofile:path]; }-(ibaction) Readbtnclick: (ID) Sender {//1. Get file path nsstring *docpath = [Nssearchpathfordirectoriesindomains (NSD    Ocumentdirectory, Nsuserdomainmask, YES) lastobject];    NSString *path = [DocPath stringbyappendingpathcomponent:@ "Stu.xxoo"]; 2. read from the fileElephant//Njperson *p = [Nskeyedunarchiver Unarchiveobjectwithfile:path];        Njstudent *stu = [Nskeyedunarchiver Unarchiveobjectwithfile:path]; NSLog (@ "%@%d%.1f%.1f", Stu.name, Stu.age, Stu.height, stu.weight);}

Nskeyedarchiver Archive if you want to save a custom object to a file, you must implement the nscoding Protocol






Overview of iOS development sandbox storage

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.