1.創建一個單例模式
//
// 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
//保存數據
-(IBAction)save{
//實現保存
NSString *savePath=[self getFilePath];
//定義data
NSMutableData *data=[[NSMutableData alloc]init ];
//定義壓縮的工具類
NSKeyedArchiver *archer=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//保存的對象
Student *stu=[[Student alloc]init];
stu.studentId=tfId.text;
stu.studentName=tfName.text;
stu.studentClass=tfClass.text;
//壓縮
[archer encodeObject:stu forKey:@"stuobj"];
[archer finishEncoding];
//寫入文件
[data writeToFile:savePath atomically:YES];
NSLog(@"保存成功");
}
//讀取數據
-(IBAction)read{
//獲取path
NSString *readPath=[self getFilePath];
//獲取文件的二進制流
NSData *data=[[NSData alloc]initWithContentsOfFile:readPath];
if (data.length>0) {
NSKeyedUnarchiver *unArchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];
//完成student對象的實例化
Student *stu=[unArchiver decodeObjectForKey:@"stuobj"];
[unArchiver finishDecoding];
//設置顯示
tfId.text=stu.studentId;
tfName.text=stu.studentName;
tfClass.text=stu.studentClass;
}
//進行解壓
}
//
-(NSString *)getFilePath{
//設置文件保存的路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//獲取documents路徑
NSString *documentPath = [paths lastObject];
//定義全路徑
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.建立一個student類
//
// Student.h
// ios24-saveObjectToFile
//
// Created by on 13-6-18.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
//要實現當前類對象,就必須實現nscoding協議
@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;
//進行歸檔編碼
-(void)encodeWithCoder:(NSCoder *)aCoder{
//將屬性
[aCoder encodeObject:studentId forKey:@"studentId"];
[aCoder encodeObject:studentName forKey:@"studentName"];
[aCoder encodeObject:studentClass forKey:@"studentClass"];
}
//對對象進行讀取讀取出來
-(id)initWithCoder:(NSCoder *)aDecoder{
//進行屬性解碼
self.studentId=[aDecoder decodeObjectForKey:@"studentId"];
self.studentName=[aDecoder decodeObjectForKey:@"studentName"];
self.studentClass=[aDecoder decodeObjectForKey:@"studentClass"];
return self;
}
@end