Ios24-data persistence-object Archiving

Source: Internet
Author: User

1. Create a singleton Mode


//

// Ios24_saveObjectToFileViewController.h

// Ios24-saveObjectToFile

//

// Created by on 13-6-18.

// Copyright 2013 _ MyCompanyName _. All rights reserved.

//

 

# Import <UIKit/UIKit. h>

 

@ Interface ios24_saveObjectToFileViewController: UIViewController <UITextFieldDelegate>

{

UITextField * tfId;

UITextField * tfName;

UITextField * tfClass;

}

@ Property (nonatomic, retain) IBOutlet UITextField * tfId;

@ Property (nonatomic, retain) IBOutlet UITextField * tfName;

@ Property (nonatomic, retain) IBOutlet UITextField * tfClass;

-(IBAction) save;

-(IBAction) read;

-(NSString *) getFilePath;

@ End

 

----------------------------------------------

 


//

// Ios24_saveObjectToFileViewController.m

// Ios24-saveObjectToFile

//

// Created by on 13-6-18.

// Copyright 2013 _ MyCompanyName _. All rights reserved.

//

 

# Import "ios24_saveObjectToFileViewController.h"

# Import "Student. h"

@ Implementation ios24_saveObjectToFileViewController

@ Synthesize tfId, tfName, tfClass;

-(Void) didReceiveMemoryWarning

{

[Super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}

 

# Pragma mark-View lifecycle

// Save data

-(IBAction) save {

// Save

NSString * savePath = [self getFilePath];

// Define data

NSMutableData * data = [NSMutableData alloc] init];

// Define the compressed tool class

NSKeyedArchiver * archer = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];

// Saved object

Student * stu = [[Student alloc] init];

Stu. studentId = tfId. text;

Stu. studentName = tfName. text;

Stu. studentClass = tfClass. text;

// Compression


[Archer encodeObject: stu forKey: @ "stuobj"];

[Archer finishEncoding];

// Write a file

[Data writeToFile: savePath atomically: YES];

NSLog (@ "saved successfully ");



}

// Read data

-(IBAction) read {

// Obtain the path

NSString * readPath = [self getFilePath];

// Obtain the binary stream of the object

NSData * data = [[NSData alloc] initWithContentsOfFile: readPath];

If (data. length> 0 ){

NSKeyedUnarchiver * unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];

// Instantiate the student object

Student * stu = [unArchiver decodeObjectForKey: @ "stuobj"];

[UnArchiver finishDecoding];

// Set display

TfId. text = stu. studentId;

TfName. text = stu. studentName;

TfClass. text = stu. studentClass;

 

}

// Decompress the package


}

//

-(NSString *) getFilePath {

// Set the path for saving the file

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );


// Obtain the sources path

NSString * documentPath = [paths lastObject];

// Define the full path

NSString * savePath = [documentPath stringByAppendingPathComponent: @ "student. achiver"];

Return savePath;

 

}

-(BOOL) textFieldShouldReturn :( UITextField *) textField {

[TextField resignFirstResponder];

Return YES;

}

-(Void) viewDidLoad

{

[Super viewDidLoad];

TfId. delegate = self;

TfName. delegate = self;

TfClass. delegate = self;

// Do any additional setup after loading the view, typically from a nib.

}

 

-(Void) viewDidUnload

{

[Super viewDidUnload];

// Release any retained subviews of the main view.

// E.g. self. myOutlet = nil;

}

 

-(Void) viewWillAppear :( BOOL) animated

{

[Super viewWillAppear: animated];

}

 

-(Void) viewDidAppear :( BOOL) animated

{

[Super viewDidAppear: animated];

}

 

-(Void) viewWillDisappear :( BOOL) animated

{

[Super viewWillDisappear: animated];

}

 

-(Void) viewDidDisappear :( BOOL) animated

{

[Super viewDidDisappear: animated];

}

 

-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation

{

// Return YES for supported orientations

Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );

}

 

@ End

 

2. Create a student class

//

// Student. h

// Ios24-saveObjectToFile

//

// Created by on 13-6-18.

// Copyright 2013 _ MyCompanyName _. All rights reserved.

//

 

# Import <Foundation/Foundation. h>

// To implement the current class object, the nscoding protocol must be implemented.

@ Interface Student: NSObject <NSCoding>

{

NSString * studentId;

NSString * studentName;

NSString * studentClass;

}

@ Property (nonatomic, retain) NSString * studentId;

@ Property (nonatomic, retain) NSString * studentName;

@ Property (nonatomic, retain) NSString * studentClass;

@ End

 

------------------------------

 

 

 

//

// Student. m

// Ios24-saveObjectToFile

//

// Created by on 13-6-18.

// Copyright 2013 _ MyCompanyName _. All rights reserved.

//

 

# Import "Student. h"

 

@ Implementation Student

@ Synthesize studentId, studentName, studentClass;

// Perform archive Encoding

-(Void) encodeWithCoder :( NSCoder *) ACO {

// Set attributes

[Ecoder encodeObject: studentId forKey: @ "studentId"];

[ACO der encodeObject: studentName forKey: @ "studentName"];

[ACO der encodeObject: studentClass forKey: @ "studentClass"];

}

// Read the object

-(Id) initWithCoder :( NSCoder *) aDecoder {

// Perform attribute Decoding

Self. studentId = [aDecoder decodeObjectForKey: @ "studentId"];

Self. studentName = [aDecoder decodeObjectForKey: @ "studentName"];

Self. studentClass = [aDecoder decodeObjectForKey: @ "studentClass"];

Return self;

}

@ End

 

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.