归档




// Dog.h
#import <Foundation/Foundation.h>

@interface Dog : NSObject<NSCoding>

/** 名称 */
@property (nonatomic ,strong) NSString *name;

/** 体重 */
@property (nonatomic ,strong) NSString *weight;


@end

// Dog.m
#import "Dog.h"


@implementation Dog

// 归档哪些属性
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.weight forKey:@"weight"];
}

// 解档哪些属性
// 当解析一个文件的时候就会调用
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{

    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.weight = [aDecoder decodeObjectForKey:@"weight"];
    }
    return self;
}

@end

// Person.h
#import <Foundation/Foundation.h>

@class Dog;
@interface Person : NSObject<NSCoding>

/** 姓名 */
@property (nonatomic ,strong) NSString *name;
/** 年龄 */
@property (nonatomic, assign) NSInteger age;
/** 狗 */
@property (nonatomic ,strong) Dog *dog;

@end

// Person.m
#import "Person.h"

@implementation Person

//归档哪些属性.
-(void)encodeWithCoder:(NSCoder *)aCoder {

    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
    [aCoder encodeObject:self.dog forKey:@"dog"];
}


//解档哪些属性
//当解析一个文件的时候就会调用.
-(instancetype)initWithCoder:(NSCoder *)aDecoder {

    if (self = [super init]) {
      self.name =   [aDecoder  decodeObjectForKey:@"name"];
      self.age =   [aDecoder decodeIntegerForKey:@"age"];
      self.dog = [aDecoder decodeObjectForKey:@"dog"];
    }
    return self;
}

@end

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end


// ViewController.m
#import "ViewController.h"
#import "Person.h"
#import "Dog.h"
@interface ViewController ()

@end

@implementation ViewController

- (IBAction)save:(id)sender {

    Person *per =  [[Person alloc] init];
    per.name = @"xmg";
    per.age = 10;

    Dog *dog = [[Dog alloc] init];
    dog.name = @"ww";
    per.dog = dog;

    //获取目录
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    //拼接路径
    NSString *filePath = [path stringByAppendingPathComponent:@"person.data"];

    //归档
    //archiveRootObject底层会调用encodeWithCoder
    //就是要告诉它归档哪些属性.
    [NSKeyedArchiver archiveRootObject:per toFile:filePath];
}
- (IBAction)read:(id)sender {

    //获取目录
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    //拼接路径
    NSString *filePath = [path stringByAppendingPathComponent:@"person.data"];

    //解档unarchiveObjectWithFile会调用initWithCoder
    //获取当前这个对象的哪些属性.
    Person *per =  [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"%@",per.name);
    NSLog(@"%ld",per.age);
    NSLog(@"%@",per.dog.name);
    NSLog(@"%@", per.dog.weight);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@", NSHomeDirectory());
}

@end

  • initWithCoder:浅析

// VCView.h
#import <UIKit/UIKit.h>

@interface VCView : UIView

@end

// VCView.m
#import "VCView.h"

@interface VCView()


@property (weak, nonatomic) IBOutlet UIButton *btn;

@end


@implementation VCView

// 从nib文件当中加载完毕时调用 2
-(void)awakeFromNib {
    [super awakeFromNib];
    NSLog(@"%@",self.btn);
}

// 解析文件时会调用 1
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    if (self = [super initWithCoder:aDecoder]) { // UIView遵守了<NSCoding>协议,所以这里用了initWithCoder
        NSLog(@"===%@",self.btn);
    }
    
    return self;
}

@end




// XCFMyInfo.h
//  XCFAuthorDetail : @interface XCFAuthorDetail : NSObject <NSCoding>

#import <Foundation/Foundation.h>
@class XCFAuthorDetail;

@interface XCFMyInfo : NSObject

/**
 *  @return 快速获取XCFAuthorDetail对象
 */
+ (XCFAuthorDetail *)info;

/**
 *  更新最新值
 *
 *  @param info 传入的最新的对象
 */
+ (void)updateInfoWithNewInfo : (XCFAuthorDetail *)info;
@end

// XCFMyInfo.m
#import "XCFMyInfo.h"
#import "XCFAuthorDetail.h"

@implementation XCFMyInfo

static XCFAuthorDetail *_myInfo;
static NSString *const kMyInfo = @"myInfo";

+ (XCFAuthorDetail *)info
{
    // 从偏好设置中取数据
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kMyInfo];
    // 解档 另外一种方式[NSKeyedUnarchiver unarchiveObjectWithFile:]
    _myInfo = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    // 判断
    if (_myInfo == nil) {
        _myInfo = [[XCFAuthorDetail alloc] init];
        _myInfo.type        = XCFAuthorTypeMe;
        _myInfo.nfollow     = @"1";
        _myInfo.nfollowed   = @"99999";
        _myInfo.create_time = @"1970-01-01 13:10:10";
        _myInfo.ndishes     = @"520";
        _myInfo.nrecipes    = @"1314";
    }
    return _myInfo;
}

+ (void)updateInfoWithNewInfo : (XCFAuthorDetail *)info
{
    // 归档
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:info];
    // 存到偏好设置中
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:kMyInfo];
    // 立即写入
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容