IOS data persistence 2 (archiving and anti-archiving)

Source: Internet
Author: User

Data Persistence is actually to store data on the network or hard disk, which is stored on the local hard disk. The local hard disk of the application is a sandbox, and the sandbox is actually a folder, it contains four folders. These are the documents, library, app package, and TMP folders. The documents mainly store files that users have been using for a long time. The library contains the caches and preferences folders, caches stores temporary files and caches them. Preferences stores preferences. TMP is a temporary file, but it is different from caches. Some files are compiled in the app package and cannot be modified.


First, create a person class and define three attributes.

. H file

# Import
<Foundation/Foundation. h>

@ Interface person:
Nsobject <nscoding>

{


Nsstring * Name;


Int age;


Nsstring * phone;

}

@ Property (nonatomic, retain) nsstring
* Name, * phone;

@ Property (nonatomic, assign) int age;

-(ID) initwithname :( nsstring *) aname phone :( nsstring *) aphone age :( INT) Aage; // Initialization Method

@ End

. M file

# Import
"Person. H"

@ Implementation person

@ Synthesize name, age, phone;

-(ID) initwithname :( nsstring *) aname phone :( nsstring *) aphone age :( INT) Aage

{

[Super
Init];


If (Self ){


Self. Name = aname;


Self. Phone = aphone;


Self. Age = Aage;

}


Return self;

}

-(Void) encodewithcoder :( nscoder *) acder // encode the attribute

{

[ACO
Encodeobject: Self. Name
Forkey: @ "name"];

[ACO
Encodeobject: Self. Phone
Forkey: @ "phone"];

[ACO
Encodeinteger: Self. Age
Forkey: @ "Age"];

}

-(ID) initwithcoder :( nscoder *) adecoder // decodes attributes

{


Nsstring * name1 = [adecoder decodeobjectforkey: @ "name"];


Nsstring * phone1 = [adecoder decodeobjectforkey: @ "phone"];


Int age1 = [adecoder decodeintegerforkey: @ "Age"];

[Self
Initwithname: name1
Phone: phone1 age: age1];


Return self;

}

@ End

Archive and archive in the main program

-(Void) loadview

{

Self. view = [[uiview alloc] initwithframe: cgrectmake (0, 0,320,460)];

Self. View. backgroundcolor = [uicolor whitecolor];

Person * P1 = [[person alloc] initwithname: @ "Jim" Phone: @ "654789" Age: 3];

Person * P2 = [[person alloc] initwithname: @ "Tom" Phone: @ "5464" Age: 4];

// File archiving

Nsmutabledata * Data = [nsmutabledata data];

// Create an archive class

Nskeyedarchiver * archiver = [[nskeyedarchiver alloc] initforwritingwithmutabledata: Data];

[Archiver encodeobject: P1 forkey: @ "person1"];

[Archiver encodeobject: P2 forkey: @ "person2"];

[Archiver finishencoding];

[Archiver release];

// Write data to a file

[Data writetofile: [self filepath] atomically: Yes];

// Archive and retrieve data from the file

Nsmutabledata * data1 = [nsmutabledata datawithcontentsoffile: [self filepath];

Nskeyedunarchiver * unarchiver = [[nskeyedunarchiver alloc] initforreadingwithdata: data1];

Person * P3 = [unarchiver decodeobjectforkey: @ "person1"];

Person * P4 = [unarchiver decodeobjectforkey: @ "person2"];

Nslog (@ "% @ % d", p3.name, p3.phone, p3.age );

Nslog (@ "% @ % d", p4.name, p4.phone, p4.age );

}

-(Nsstring *) filepath

{

Nsstring * docpath = [nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdirectory, yes) objectatindex: 0];

Nsstring * Path = [docpath stringbyappendingpathcomponent: @ "texts"];

// Nsstring * Path = [nshomedirectory () stringbyappendingpathcomponent: @ "Library/caches/texts"]; // either of the two methods can be used

Return path;

}

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.