IOS archive: serialization and deserialization

Source: Internet
Author: User

The younger brother hasn't updated it for a long time.

Archiving in IOS is what we know about serialization and deserialization.

We can use plist to store simple data types. But what if I want to persist the Data Types I have defined?

This requires serialization of the following code.

First, you must customize your own class to inherit the nscoding interface.

----------------------------------- // I am a separation line //-----------------------------------------

# Import <Foundation/Foundation. h>

@ Interface fourlines: nsobject <nscoding, nscopying> {

}
@ Property (nonatomic, retain) nsstring * field1;
@ Property (nonatomic, retain) nsstring * field2;
@ Property (nonatomic, retain) nsstring * field3;
@ Property (nonatomic, retain) nsstring * field4;

@ End

 

# Import "fourlines. H"
# Define kfield1key @ "field1"
# Define kfield2key @ "field2"
# Define kfield3key @ "field3"
# Define kfield4key @ "field4"

@ Implementation fourlines

@ Synthesize field1;
@ Synthesize field2;
@ Synthesize field3;
@ Synthesize field4;

-(Void) encodewithcoder :( nscoder *) ACO {
[Ecoder encodeobject: field1 forkey: kfield1key];
[Ecoder encodeobject: field2 forkey: kfield2key];
[Ecoder encodeobject: field3 forkey: kfield3key];
[Ecoder encodeobject: field4 forkey: kfield4key];
}

-(ID) initwithcoder :( nscoder *) adecoder {
If (Self = [Super init]) {
Field1 = [[adecoder decodeobjectforkey: kfield1key] retain];
Field2 = [[adecoder decodeobjectforkey: kfield2key] retain];
Field3 = [[adecoder decodeobjectforkey: kfield3key] retain];
Field4 = [[adecoder decodeobjectforkey: kfield4key] retain];
}
Return self;
}

-(ID) copywithzone :( nszone *) Zone {
Fourlines * Copy = [[self class] allocwithzone: Zone] init];
Copy. field1 = [[self. field1 copywithzone: Zone] autorelease];
Copy. field2 = [[self. field2 copywithzone: Zone] autorelease];
Copy. field3 = [[self. field3 copywithzone: Zone] autorelease];
Copy. field4 = [[self. field4 copywithzone: Zone] autorelease];
Return copy;
}

-(Void) dealloc {
[Field1 release];
[Field2 release];
[Field3 release];
[Field4 release];
[Super dealloc];
}

@ End

 

The following is a controller to implement persistent custom classes.

 

# Import <uikit/uikit. h>

// # Define kfilename @ "data. plist"
# Define kfilename @ "ARCHIVE"
# Define kdatakey @ "data"
// Define kfilename @ "dataarchiive. plist"
// # Define kdatakey @ "data"
// # Define kfilename @ "data. sqlite3"

@ Interface persistenceviewcontroller: uiviewcontroller {

}
@ Property (nonatomic, retain) iboutlet uitextfield * field1;
@ Property (nonatomic, retain) iboutlet uitextfield * field2;
@ Property (nonatomic, retain) iboutlet uitextfield * field3;
@ Property (nonatomic, retain) iboutlet uitextfield * field4;

-(Nsstring *) datafilepath;
-(Void) applicationwillresignactive :( nsnotification *) notification;

@ End

 

 

# Import "persistenceviewcontroller. H"
# Import "fourlines. H"

@ Implementation persistenceviewcontroller

@ Synthesize field1;
@ Synthesize field2;
@ Synthesize field3;
@ Synthesize field4;

-(Nsstring *) datafilepath {
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0];
Nslog (@ "document path: % @", documentsdirectory );
Return [documentsdirectory stringbyappendingpathcomponent: kfilename];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) nibbundleornil {
Self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil];
If (Self ){
// Custom Initialization
}
Return self;
}
*/

/*
// Implement loadview to create a view hierarchy programmatically, without using a nib.
-(Void) loadview {
}
*/

 

// Implement viewdidload to do additional setup after loading the view, typically from a nib.
-(Void) viewdidload {
Nsstring * filepath = [self datafilepath];
If ([[nsfilemanager defaultmanager] fileexistsatpath: filepath]) {
/*
Nsarray * array = [[nsarray alloc] initwithcontentsoffile: filepath];
Field1.text = [array objectatindex: 0];
Field2.text = [array objectatindex: 1];
Field3.text = [array objectatindex: 2];
Field4.text = [array objectatindex: 3];
[Array release];
*/

// Encoding
Nsdata * Data = [[nsmutabledata alloc] initwithcontentsoffile: filepath];
Nskeyedunarchiver * unarchiver = [[nskeyedunarchiver alloc] initforreadingwithdata: Data];

Fourlines * fourlines = [unarchiver decodeobjectforkey: kdatakey];
[Unarchiver finishdecoding];

Field1.text = fourlines. field1;
Field2.text = fourlines. field2;
Field3.text = fourlines. field3;
Field4.text = fourlines. field4;

[Unarchiver release];
[Data release];
}
 
Uiapplication * APP = [uiapplication sharedapplication];
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (applicationwillresignactive :) name: uiapplicationwillresignactivenotification object: app];
[Super viewdidload];
}

-(Void) applicationwillresignactive :( nsnotification *) Notification {
/*
Nsmutablearray * array = [[nsmutablearray alloc] init];
[Array addobject: field1.text];
[Array addobject: field2.text];
[Array addobject: field3.text];
[Array addobject: field4.text];
[Array writetofile: [self datafilepath] atomically: Yes];
*/
Fourlines * fourline = [[fourlines alloc] init];
Fourline. field1 = field1.text;
Fourline. field2 = field2.text;
Fourline. field3 = field3.text;
Fourline. field4 = field4.text;
 
Nsmutabledata * Data = [nsmutabledata alloc] init];
Nskeyedarchiver * archiver = [[nskeyedarchiver alloc] initforwritingwithmutabledata: Data];
[Archiver encodeobject: fourline forkey: kdatakey];
[Archiver finishencoding];
[Data writetofile: [self datafilepath] atomically: Yes];
[Fourline release];
[Data release];
[Archiver release];
}

// Override to allow orientations other than the default portrait orientation.
-(Bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation {
Return yes;
}

-(Void) didreceivememorywarning {
// Releases the view if it doesn't have a superview.
[Super didreceivememorywarning];
 
// Release any cached data, images, etc that aren't in use.
}

-(Void) viewdidunload {
// Release any retained subviews of the main view.
// E.g. Self. myoutlet = nil;
}

-(Void) dealloc {
[Field1 release];
[Field2 release];
[Field3 release];
[Field4 release];
[Super dealloc];
}

@ End

 

I hope to help you to share with you how to use the embedded database sqlite3.

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.