详解iOS开发中app的归档以及偏好设置的存储方式

2020-01-14 18:27:16于海丽

//
//  YYstudent.h
//  02-归档
//
//  Created by apple on 14-6-7.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYPerson.h"

@interface YYstudent : YYPerson
//增加一个体重属性
@property(nonatomic,assign) double weight;
@end


YYstudent.m文件
复制代码
//
//  YYstudent.m
//  02-归档
//
//  Created by apple on 14-6-7.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYstudent.h"

@implementation YYstudent

//在子类中重写这两个方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [super encodeWithCoder:aCoder];
    NSLog(@"调用了YYStudent encodeWithCoder");
    [aCoder encodeFloat:self.weight forKey:@"weight"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        NSLog(@"调用了YYstudent initWithCoder");
        self.weight = [aDecoder decodeFloatForKey:@"weight"];
    }
    return self;
}
@end


YYViewController.m文件
复制代码
//
//  YYViewController.m
//  02-归档
//
//  Created by apple on 14-6-7.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYViewController.h"
#import "YYPerson.h"
#import "YYstudent.h"

@interface YYViewController ()
- (IBAction)saveBtnOnclick:(id)sender;
- (IBAction)readBtnOnclick:(id)sender;

@end


复制代码
@implementation YYViewController

 

- (void)viewDidLoad
{
    [super viewDidLoad];
}


- (IBAction)saveBtnOnclick:(id)sender {
    //1.创建对象