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