IOS Data Persistence

Source: Internet
Author: User

One, plist file storage

Get files

NSString *path =*filename = [path stringbyappendingpathcomponent:@ "123.plist"];

Storing data
Nsarray *array = @[@"123"@"456"@ "789 " ]; [Array writetofile:filename atomically:yes];

Reading data

Nsarray *result = [Nsarray arraywithcontentsoffile:filename]; NSLog (@ "%@", result);

Description: Use the writetofile:atomically: method when storing, use the Arraywithcontentsoffile: method when reading.

Second, preference settings

How to use

//1. Get nsuserdefaults fileNsuserdefaults *userdefaults =[Nsuserdefaults standarduserdefaults];//2. Writing content to a file[Userdefaults SetObject:@"AAA"Forkey:@"a"]; [Userdefaults setbool:yes Forkey:@"Sex"]; [Userdefaults Setinteger: +Forkey:@" Age"];//2.1 Sync now[Userdefaults synchronize];//3. Reading filesNSString *name = [Userdefaults objectforkey:@"a"]; BOOL Sex= [Userdefaults Boolforkey:@"Sex"]; Nsinteger Age= [Userdefaults Integerforkey:@" Age"]; NSLog (@"%@,%d,%ld", name, sex, age);

Instructions for use

    1. Preferences are specific configuration information that is used to save the application.
    2. Preferences will save all the data in the same file. A plist file that is named under this application package name in the preference directory.

Third, Nskeyedarchiver (archive)

1. Follow the agreement, set the properties

// 1. Follow the nscoding   agreement @interface person:nsobject<nscoding>  //2. Set Property @property (Strong, nonatomic) UIImage *  *name; @property (assign, nonatomic) Nsinteger age;  @end

2. Implement the Protocol method

-(ID) Initwithcoder: (Nscoder *) Adecoder {if([Super init]) {Self.avatar= [Adecoder Decodeobjectforkey:@"Avatar"]; Self.name= [Adecoder Decodeobjectforkey:@"name"]; Self.age= [Adecoder Decodeintegerforkey:@" Age"]; }      returnSelf ; }  //Archive- (void) Encodewithcoder: (Nscoder *) Acoder {[Acoder encodeObject:self.avatar forkey:@"Avatar"]; [Acoder encodeObject:self.name Forkey:@"name"]; [Acoder encodeInteger:self.age Forkey:@" Age"]; }

3. Use

(1) The need to archive objects is the factory method that calls Nskeyedarchiver Archiverootobject:tofile: Method

(2) A factory method that calls Nskeyedunarchiver is called from the file to be the object of the document Unarchiveobjectwithfile: you can

Description
    1. If the class that needs to be archived is a subclass of a custom class, you need to implement the archive and file method of the parent class before archiving and reconciling the files. [Super Encodewithcoder:acoder] and [Super Initwithcoder:adecoder] method;
    2. Archiving is another form of serialization in iOS, as long as objects that follow the Nscoding protocol can be serialized through it. Because the majority of foundation and cocoa touch classes that support storage data follow the Nscoding protocol

IOS Data Persistence

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