ios-day12-04(ios开发中数据存储之NSKeyedArchiver归档(NSCoding)、普通对象如何归档、普通对象的子类如何归档)

源码下载地址: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值