----------------------------File.h-------------------------------
#import <Foundation/Foundation.h>
@interface FileHelper : NSObject
+(NSMutableArray *)loadData;//加载数据并保存在数组中
+(void)saveStudentData:(NSMutableArray *)dataArr;//保存数据在数组中
+(NSMutableArray *)loadStudentData;//以学生为例
@end
----------------------------------------------------------------
----------------------------File.m-----------------------------
#import "File.h"
/**设置文件保存的路径*/
static NSString * DATA_INFO_FILE = (NSString *)path;
@implementation File
//加载文件,
+(NSMutableArray *)loadDataFromFile:(NSString *)filename{
return [NSKeyedUnarchiver unarchiveObjectWithFile:filename];
}
//保存文件
+(void)saveData:(NSMutableArray *)dataArr toFile:(NSString *)filename{
[NSKeyedArchiver archiveRootObject:dataArr toFile:filename];
}
//加载学生数据
+(NSMutableArray *)loadStudentData{
// self 就是指调用当前方法的receiver
// 谁调用当前方法,self就指谁
return [self loadDataFromFile:STUDENT_INFO_FILE];
}
//保存学生数据
+(void)saveStudentData:(NSMutableArray *)dataArr{
[self saveData:dataArr toFile:STUDENT_INFO_FILE];
}
----------------------------------------------------------------
----------------------Student---------------------------------
#import <Foundation/Foundation.h>
@interface Student : NSObject<NSCoding>
//学号、姓名、密码、系别、班级、电话
@property(nonatomic, copy)NSString * number;
@property(nonatomic, copy)NSString * name;
@property(nonatomic, copy)NSString * password;
@property(nonatomic, copy)NSString * department;
@property(nonatomic, copy)NSString * grade;
@property(nonatomic, copy)NSString * phone;
-(id)initWithNumber:(NSString *)number andName:(NSString *)name andPassword:(NSString *)password andDepartment:(NSString *)department andGrade:(NSString *)grade andPhone:(NSString *)phone;
@end
-------------------------------------------------------------
-----------------------Student.m--------------------------
#import "Student.h"
@implementation Student
/**初始化学生对象*/
-(id)initWithNumber:(NSString *)number andName:(NSString *)name andPassword:(NSString *)password andDepartment:(NSString *)department andGrade:(NSString *)grade andPhone:(NSString *)phone{
self = [super init];
if (self) {
self.number = number;
self.name = name;
self.password = password;
self.department = department;
self.grade = grade;
self.phone = phone;
}
return self;
}
/**序列化,作用:通过ios的协议将文件保存*/
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.number forKey:@"number"];
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.password forKey:@"password"];
[aCoder encodeObject:self.department forKey:@"department"];
[aCoder encodeObject:self.grade forKey:@"grade"];
[aCoder encodeObject:self.phone forKey:@"phone"];
}
/**反序列化,作用:通过ios协议读取文件*/
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
self.number = [aDecoder decodeObjectForKey:@"number"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.password = [aDecoder decodeObjectForKey:@"password"];
self.department = [aDecoder decodeObjectForKey:@"department"];
self.grade = [aDecoder decodeObjectForKey:@"grade"];
self.phone = [aDecoder decodeObjectForKey:@"phone"];
}
return self;
}
/**打印出学生基本信息*/
-(NSString *)description{
return [NSString stringWithFormat:@"%@ %@ %@ %@ %@", self.number, self.name, self.department, self.grade, self.phone];
}
@end
----------------------------------------------------------
---------------Start.h-----------------------------------
+(void)saveDataAfterAppDown;
+(void)loadDataBeforeAppRun;
----------------------------------------------------------
----------------------Start.m---------------------------
+(void)loadDataBeforeAppRun
{
/**加载学生信息文件并初始化一个学生信息数组,用来保存学生的基本信息*/
studentArr = [FileHelper loadStudentData];
if (studentArr == nil) {
studentArr = [[NSMutableArray alloc]init];
}
}
/**保存本次的数据*/
+(void)saveDataAfterAppDown{
NSLog(@"系统退出,正在保存信息。。。");
[File saveStudentData:studentArr];
}
----------------------------------------------------------