Operation of files in OC (loading existing file data, accessing files)

Source: Internet
Author: User

----------------------------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)

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.