Three easy ways to "iOS Dev-75" ios data storage: plist, preference, and nscoding storage objects

Source: Internet
Author: User

In the actual development, the storage data is mainly using SQLite. In practice, we mainly use the following three kinds of storage methods.

(1) using plist storage to simply NSString, Nsarray, nsdictionary, etc.

(2) Using preference storage, similar to the above, the storage is simple data, essentially a plist file.

(3) Using Nscoding storage objects These complex data are essentially a data file that requires the stored classes to adhere to the Nscoding protocol and implement the Init and encode methods.

The code is as follows:


-(void) Viewdidload {///The data we save is primarily present in documents and will be erased by itunes backup//tmp data may be removed at any time//library the caches saved in the cache, will not be erased, but will not be purged by the ITU    NES backup//library in preference saved preferences, will be itunes backup//1, save to plist file NSString *homepath=nshomedirectory ();    NSString *docpath=[homepath stringbyappendingpathcomponent:@ "Documents"];    NSString *filepath=[docpath stringbyappendingpathcomponent:@ "Data2plist.plist"];    Nsarray *[email protected][@ "AAA", @10,@ "Hello";    [Array Writetofile:filepath atomically:yes];    1, read plist file Nsarray *arrayout=[nsarray Arraywithcontentsoffile:filepath];    for (int i=0; i<arrayout.count; i++) {NSLog (@ "%@", Arrayout[i]); }//2, save to preference, compared to plist, is less the path, because this path is the Red copper automatically judge the Nsuserdefaults *defaults=[nsuserdefaults standarduserdefaults    ];    [Defaults setobject:@ "Hello" forkey:@ "1"];    [Defaults setbool:yes forkey:@ "Yue"];    [Defaults setinteger:10 forkey:@ "age"];    Sync now (good habits) [defaults synchronize]; 2. Read Data nsstring *str1=[defaults OBjectforkey:@ "1"];    BOOL bool1=[defaults boolforkey:@ "Yue"];    int integer1=[defaults integerforkey:@ "age"];    NSLog (@ "%@,%d,%i", str1,bool1,integer1);    It should be noted above that the preference data is stored in the same place as the XCODE5 Simulator//3, with nscoding to save the object Wpperson *p1=[[wpperson Alloc]init];    [email protected] "Andy";    p1.age=30;    NSString *filepath2=[docpath stringbyappendingpathcomponent:@ ""];    [Nskeyedarchiver archiverootobject:p1 tofile:filepath2];    3, read data Wpperson *p2=[nskeyedunarchiver unarchiveobjectwithfile:filepath2];        NSLog (@ "%@,%i",,p2.age);    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.}

--For a third example, create a Wpperson class that inherits from the simple class of nsobject.

In WPPerson.h:

#import <Foundation/Foundation.h> @interface wpperson:nsobject<nscoding> @property (nonatomic,copy) NSString *name; @property (nonatomic,assign) int age; @end


#import "WPPerson.h" @implementation wpperson-(void) Encodewithcoder: (Nscoder *) acoder{    [Acoder encodeobject:_ Name forkey:@ "Name"];    [Acoder encodeint:_age forkey:@ "Age"];} -(ID) Initwithcoder: (Nscoder *) adecoder{    if (self=[super init]) {        _name=[adecoder decodeobjectforkey:@ "name" ];        _age=[adecoder decodeintforkey:@ "Age"];    }    return self;} @end

Three easy ways to "iOS Dev-75" ios data storage: plist, preference, and nscoding storage objects

Related Article

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: 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.