----------------------------File.h-------------------------------
#import <Foundation/Foundation.h>
@interface Filehelper:nsobject
+ (Nsmutablearray *) loaddata;//load data and save in array
+ (void) Savestudentdata: (Nsmutablearray *) dataarr;//save data in array
+ (Nsmutablearray *) loadstudentdata;//take students as an example
@end
----------------------------------------------------------------
----------------------------FILE.M-----------------------------
#import "File.h"
/** Set File Save path */
Static NSString * Data_info_file = (NSString *) path;
@implementation File
Load files,
+ (Nsmutablearray *) Loaddatafromfile: (NSString *) filename{
return [Nskeyedunarchiver Unarchiveobjectwithfile:filename];
}
Save File
+ (void) SaveData: (Nsmutablearray *) Dataarr tofile: (NSString *) filename{
[Nskeyedarchiver Archiverootobject:dataarr Tofile:filename];
}
Load Student Data
+ (Nsmutablearray *) loadstudentdata{
Self means receiver that invokes the current method.
Who calls the current method, self means who
return [self loaddatafromfile:student_info_file];
}
Save Student data
+ (void) Savestudentdata: (Nsmutablearray *) dataarr{
[Self Savedata:dataarr tofile:student_info_file];
}
----------------------------------------------------------------
----------------------Student---------------------------------
#import <Foundation/Foundation.h>
@interface student:nsobject<nscoding>
School number, name, password, department, class, telephone
@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
/** Initializing student Objects */
-(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;
}
/** serialization, Function: Save the file via the iOS protocol */
-(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"];
}
/** deserialization, function: Read files via iOS protocol */
-(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;
}
/** Print out basic student information * *
-(NSString *) description{
return [NSString stringwithformat:@ "%@%@%@%@%@", Self.number, Self.name, Self.department, Self.grade, self.ph One];
}
@end
----------------------------------------------------------
---------------Start.h-----------------------------------
+ (void) Savedataafterappdown;
+ (void) Loaddatabeforeapprun;
----------------------------------------------------------
----------------------START.M---------------------------
+ (void) Loaddatabeforeapprun
{
/** loads the student information file and initializes an array of student information to hold the student's basic information * *
Studentarr = [Filehelper loadstudentdata];
if (Studentarr = = nil) {
Studentarr = [[Nsmutablearray alloc]init];
}
}
/** Save the data for this time * *
+ (void) savedataafterappdown{
NSLog (@ "System exited, saving information ... ");
[File Savestudentdata:studentarr];
}
----------------------------------------------------------
Operation of files in OC (loading existing file data, accessing files)