////////////////////
Student.h ////////////////////
#import
// 1.
如果想要該類型的對(duì)象可以進(jìn)行持久化數(shù)據(jù)保存,,需要使該類遵守 協(xié)議
@interface
Student
: NSObject <</span>NSCoding>
#pragma mark - 聲明屬性
@property (nonatomic,
copy)
NSString
*name;
//
姓名
@property (nonatomic,
copy)
NSString
*gender;
//
性別
@property (nonatomic,
assign)
NSInteger
age;
//
年齡
#pragma mark - 聲明方法
#pragma mark 初始化方法
- (instancetype)initWithName:(NSString
*)name
gender:(NSString
*)gender
age:(NSInteger)age;
#pragma mark 遍歷構(gòu)造器
+ (instancetype)studentWithName:(NSString
*)name
gender:(NSString
*)gender
age:(NSInteger)age;
@end
//////////////////// Student.m ////////////////////
#import
"Student.h"
#define KEY_NAME @"name"
#define KEY_GENDER @"gender"
#define KEY_AGE @"age"
@implementation Student
// 2.
遵守了協(xié)議之后,,需要實(shí)現(xiàn)該協(xié)議中必須實(shí)現(xiàn)的方法
#pragma mark - NSCoding Methods
#pragma mark 編碼的方法
- (void)encodeWithCoder:(NSCoder
*)aCoder
{
//
將各個(gè)屬性根據(jù)key值進(jìn)行編碼
[aCoder encodeObject:_name
forKey:KEY_NAME];
[aCoder encodeObject:_gender
forKey:KEY_GENDER];
[aCoder encodeInteger:_age
forKey:KEY_AGE];
}
#pragma mark 解碼的方法
- (id)initWithCoder:(NSCoder
*)aDecoder
{
if
(self
= [super
init])
{
//
根據(jù)key值進(jìn)行解碼
self.name
= [aDecoder decodeObjectForKey:KEY_NAME];
self.gender
= [aDecoder decodeObjectForKey:KEY_GENDER];
self.age
= [aDecoder decodeIntegerForKey:KEY_AGE];
}
return self;
}
#pragma mark - 實(shí)現(xiàn)方法
#pragma mark 初始化方法
- (instancetype)initWithName:(NSString
*)name
gender:(NSString
*)gender
age:(NSInteger)age
{
if
(self
= [super
init])
{
self.name
= name;
self.gender
= gender;
self.age
= age;
}
return self;
}
#pragma mark 遍歷構(gòu)造器
+ (instancetype)studentWithName:(NSString
*)name
gender:(NSString
*)gender
age:(NSInteger)age
{
return
[[[[self
class]
alloc]
initWithName:name
gender:gender
age:age]
autorelease];
}
#pragma mark - 重寫
#pragma mark dealloc
- (void)dealloc
{
[_name release];
[_gender release];
[super
dealloc];
}
@end
//////////////////// ViewController.h ////////////////////
#import
@interface
ViewController : UIViewController
@end
//////////////////// ViewController.m /////////////////////
#import
"ViewController.h"
#import
"Student.h"
@interface
ViewController ()
@end
@implementation
ViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
// 沙盒中Documents目錄
NSString
*documentsPath
= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES).lastObject;
// 1. 創(chuàng)建模型對(duì)象
Student
*stu1
= [Student
studentWithName:@"xiaoming"
gender:@"man"
age:18];
Student
*stu2
= [Student
studentWithName:@"xiaohong"
gender:@"woman"
age:19];
// 2. 將學(xué)生對(duì)象進(jìn)行歸檔,,添加到NSMutableData中
// 2.1
先創(chuàng)建出來(lái)可變的數(shù)據(jù)對(duì)象
NSMutableData *mutableData
= [NSMutableData data];
// 2.2
使用數(shù)據(jù)對(duì)象創(chuàng)建出歸檔工具
NSKeyedArchiver
*archiver
= [[[NSKeyedArchiver
alloc]
initForWritingWithMutableData:mutableData]
autorelease];
// 2.3 使用歸檔工具,進(jìn)行歸檔
[archiver encodeObject:stu1
forKey:@"stu1"];
[archiver encodeObject:stu2
forKey:@"stu2"];
// 2.4 需要說(shuō)明,,歸檔完成
[archiver finishEncoding];
// 3.
將歸檔好的學(xué)生數(shù)據(jù),,寫入到文件中
// 3.1 拼接出文件路徑
NSString
*studentsFilePath = [documentsPath stringByAppendingString:@"/student.data"];
// 3.2 將數(shù)據(jù)寫入
[mutableData writeToFile:studentsFilePath
atomically:YES];
// 4.
從沙盒中,將寫入的數(shù)據(jù)讀取出來(lái)
// 4.1 首先讀取出保存的數(shù)據(jù)
NSData
*savedData = [NSData
dataWithContentsOfFile:studentsFilePath];
// 4.2
使用讀取到的數(shù)據(jù)創(chuàng)建反歸檔的數(shù)據(jù)
NSKeyedUnarchiver
*unarchiver = [[[NSKeyedUnarchiver
alloc]
initForReadingWithData:savedData]
autorelease];
// 4.3 進(jìn)行反歸檔
Student
*stu3 = [unarchiver decodeObjectForKey:@"stu1"];
Student
*stu4 = [unarchiver decodeObjectForKey:@"stu2"];
// 4.4 反歸檔完畢
[unarchiver finishDecoding];
// 4.5 使用獲取到的數(shù)據(jù)
NSLog(@"取出的對(duì)象1:
%@ %@ %ld",
stu3.name,
stu3.gender,
stu3.age);
NSLog(@"取出的對(duì)象2:
%@ %@ %ld",
stu4.name,
stu4.gender,
stu4.age);
//
歸檔與反歸檔的建議寫法
Student
*stu5
= [Student
studentWithName:@"honghong"
gender:@"woman"
age:19];
Student
*stu6
= [Student
studentWithName:@"heihei"
gender:@"man"
age:20];
// 拼接文件路徑
NSString
*students2FilePath = [documentsPath stringByAppendingString:@"/students2.data"];
// 1.
使用歸檔工具直接進(jìn)行歸檔
[NSKeyedArchiver
archiveRootObject:@[stu5,
stu6]
toFile:students2FilePath];
// 2. 從沙盒中讀取路徑
NSArray
*array = [NSKeyedUnarchiver
unarchiveObjectWithFile:students2FilePath];
// 2.1 遍歷答應(yīng)學(xué)生
for
(Student
*stu in
array) {
NSLog(@"取出的對(duì)象:
%@
%@ %ld", stu.name,
stu.gender,
stu.age);
}
}
@end
|