源码下载地址:http://download.csdn.net/detail/liu537192/8505717
效果图:
核心代码:
//
// JLViewController.m
// 04-NSCoding
//
// Created by XinYou on 15-3-16.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLViewController.h"
#import "JLPerson.h"
#import "JLStudent.h"
#define JLPersonPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"person.plist"]
#define JLStudentPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.plist"]
@interface JLViewController ()
/**
* “存储Person数据”按钮
*/
- (IBAction)savePersonBtnClick;
/**
* “读取Person数据”按钮
*/
- (IBAction)readPersonBtnClick;
/**
* “存储Student数据”按钮
*/
- (IBAction)saveStudentBtnClick;
/**
* “读取Student数据”按钮
*/
- (IBAction)readStudentBtnClick;
/**
* 用于显示Person数据的label
*/
@property (weak, nonatomic) IBOutlet UILabel *personDataLabel;
/**
* 用于显示Student数据的label
*/
@property (weak, nonatomic) IBOutlet UILabel *studentDataLabel;
@end
@implementation JLViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)savePersonBtnClick {
// 新建模型对象
JLPerson *person = [[JLPerson alloc] init];
person.name = @"zhangsan";
person.age = 33;
// NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"person.plist"];
// 归档(存储)模型对象
[NSKeyedArchiver archiveRootObject:person toFile:JLPersonPath];
}
- (IBAction)readPersonBtnClick {
// NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"person.plist"];
// 从文件中读取模型对象
JLPerson *person = [NSKeyedUnarchiver unarchiveObjectWithFile:JLPersonPath];
// NSLog(@"%@ - %d", person.name, person.age);
NSString *data = [NSString stringWithFormat:@"Person name is %@, age is %d", person.name, person.age];
self.personDataLabel.text = data;
}
- (IBAction)saveStudentBtnClick {
JLStudent *stu = [[JLStudent alloc] init];
stu.name = @"lisi";
stu.age = 23;
stu.stuID = @"182";
[NSKeyedArchiver archiveRootObject:stu toFile:JLStudentPath];
}
- (IBAction)readStudentBtnClick {
JLStudent *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:JLStudentPath];
// NSLog(@"%@ - %d - %@", stu.name, stu.age, stu.stuID);
NSString *data = [NSString stringWithFormat:@"Student name is %@, age is %d, stuID is %@", stu.name, stu.age, stu.stuID];
self.studentDataLabel.text = data;
}
@end
//
// JLPerson.h
// 04-NSCoding
//
// Created by XinYou on 15-3-16.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@interface JLPerson : NSObject
@property (nonatomic, copy)NSString *name;
@property (nonatomic, assign)int age;
@end
//
// JLPerson.m
// 04-NSCoding
//
// Created by XinYou on 15-3-16.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLPerson.h"
@implementation JLPerson
#pragma mark -NSCoding的方法
/**
* 将某个对象写入文件时会调用
* 在这个方法中说清楚哪些属性需要存储
*/
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInt:self.age forKey:@"age"];
}
/**
* 从文件中解析对象时会调用
* 在这个方法中说清楚属性该如何读取
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
}
return self;
}
@end
//
// JLStudent.h
// 04-NSCoding
//
// Created by XinYou on 15-3-16.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLPerson.h"
@interface JLStudent : JLPerson
/**
* 学号
*/
@property (nonatomic, copy)NSString *stuID;
@end
//
// JLStudent.m
// 04-NSCoding
//
// Created by XinYou on 15-3-16.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLStudent.h"
@implementation JLStudent
- (void)encodeWithCoder:(NSCoder *)aCoder{
// 先调用父类的方法
[super encodeWithCoder:aCoder];
[aCoder encodeObject:self.stuID forKey:@"stuID"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
// 先调用父类的方法
if (self = [super initWithCoder:aDecoder]) {
self.stuID = [aDecoder decodeObjectForKey:@"stuID"];
}
return self;
}
@end