Simple summary of data storage in iOS

Source: Internet
Author: User

1. Nskeyedarchiver (encrypted form) 2.  Plist 3.  Nsuserdefaults 4.  WriteToFile 5. SQLite3

= = = Nskeyedarchiver ========================================-------CKPerson.h Code

@interface Ckperson:nsobject

@property (nonatomic, copy) NSString *name; @property (nonatomic, assign) int age;-(void) Encodewithcoder: (Nscoder *) acoder;-(ID) Initwithcoder: (Nscoder *) Adecoder;

@end-------ckperson.m Code-(void) Encodewithcoder: (Nscoder *) acoder{    [Acoder encodeObject:self.name Forkey : @ "name"];    [Acoder encodeInteger:self.age forkey:@ "Age"];} -(ID) Initwithcoder: (Nscoder *) adecoder{    if (self = [super init])     {        SE Lf.name = [Adecoder decodeobjectforkey:@ "name"];        self.age = [Adecoder decodeintegerforkey:@] Age "];   }    return to self;} ----------ckviewcontroller.m-(ibaction) Onsavedataclick: (UIButton *) sender {    Ckperson *p = [[Ckperson Alloc] init];    p.name = @ "Goldenkey";    p.age = 21;        NSString *DOCPA th = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastobject];    NSString *filename = FilePath = [DocPath stringbyappendingpathcomponent:@ "Student.hehe"];    [ Nskeyedarchiver Archiverootobject:p Tofile:filepath];Save Data}-(ibaction) Ongetdataclick: (UIButton *) Sender {Ckperson *p = [Nskeyedunarchiver Unarchiveobjectwithfile:filepa TH];Get Data}

= = = = Plist Access array, dictionary ===================================-(ibaction) Onarraysaveclick: (UIButton *) Sender {    Nsarray *testarray = @[@ "111", @ "121", @ "131", @ "141", @ "151"]; [Testarray WriteToFile:self.path4Array atomically:yes];} -(Ibaction) Onarraygetclick: (UIButton *) Sender {Nsarray *testarray = [Nsarray arrayWithContentsOfFile:self.path4Array    ]; NSLog (@ "Array length is%d", [Testarray Count]);}    -(Ibaction) Ondictionarysaveclick: (UIButton *) Sender {nsdictionary *dict = @{@ "name": @ "key", @ "age": @12}; [Dict writeToFile:self.path4Dict atomically:yes];} -(Ibaction) Ondictionarygetclick: (UIButton *) Sender {nsdictionary *dict = [Nsdictionary dictionarywithcontentsoffile:    SELF.PATH4DICT];    NSLog (@ "name:%@", dict[@ "name"]); NSLog (@ "age:%@", dict[@ "age"]);}

= = Nsuserdefaults Program ticket set ================================= nsuserdefaults *userdefaults = [Nsuserdefaults standardUs        Erdefaults];    [Userdefaults setinteger:1 forkey:@ "Testint"];        [Userdefaults Synchronize];    int i = [userdefaults integerforkey:@ "Testint"];    NSLog (@ "i=%d", I);    -----------NSString *name = @ "Goldenkey";    [Userdefaults setobject:name forkey:@ "name"];        [Userdefaults Synchronize];    NSString *nameresult = [userdefaults objectforkey:@ "name"]; NSLog (@ "%@", Nameresult);

= = = WriteToFile ============================================   -- Read string----------------------------------------------------    Nsarray *documentpaths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory,  nsuserdomainmask,yes);    NSString * Ourdocumentpath =[documentpaths objectatindex:0];       //Second step: Generate file under this path:    NSString *filename=[ourdocumentpath stringbyappendingpathcomponent:@ "Test.hehe"];    NSString *texts = @ " Test string ";    nsdata *data = [Texts datausingencoding:nsutf8stringencoding];    [data writetofile : filename atomically:yes];//writes NSData type Object data to a file named filename           // Read mode 1   //nsdata *dataresult=[nsdata datawithcontentsoffile:filename options:0 error:null];// read out data from filename    //nsstring *strresult = [[NSString alloc] Initwithdata:dataresult encoding: nsutf8stringencoding];    &nbsP  //Read mode 2    nsstring *strresult = [nsstring stringwithcontentsoffile:filename encoding: Nsutf8stringencoding error:nil];    NSLog (@ "%@", strresult);       //-- Access dictionary-------------------------------------------        nsstring *dictfilename = [ Ourdocumentpath stringbyappendingpathcomponent:@ "Dict.hehe"];    nsdictionary *dictTest = @{@ "name": @ " Goldenkey ", @" age ":@24};    nsdata *dictdata = [nsjsonserialization datawithjsonobject:dicttest options: nsjsonwritingprettyprinted error:nil];    [dictdata writetofile:dictfilename atomically:YES];         NSData *dataresult = [NSData datawithcontentsoffile:dictfilename];    Nsdictionary * Dictresult = [nsjsonserialization jsonobjectwithdata:dataresult options:nsjsonreadingmutablecontainers Error:nil];     NSLog (@ "name =%@", dictresult[@ "name"]); = = Sqlite3 ========================================================

Simple summary of data storage in 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.