美文网首页
自定义对象数据持久化

自定义对象数据持久化

作者: 景彧 | 来源:发表于2016-10-14 15:44 被阅读49次
//
//  Person.h
//  自定义对象数据持久化
//
//  Created by shimei on 7/29/16.
//  Copyright © 2016 Shimei. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject <NSCoding>
@property (assign, nonatomic) NSInteger  pid;
@property (copy,   nonatomic) NSString  *name;
@property (assign, nonatomic) NSInteger  age;
@property (copy,   nonatomic) NSString  *gender;

- (instancetype)initWithPid:(NSInteger)pid
                      name:(NSString *)name
                        age:(NSInteger)age
                     gender:(NSString *)gender;

- (instancetype)initWithDictionary:(NSDictionary *)dict;

@end
//
//  Person.m
//  自定义对象数据持久化
//
//  Created by shimei on 7/29/16.
//  Copyright © 2016 Shimei. All rights reserved.
//

#import "Person.h"

@implementation Person
- (instancetype)initWithPid:(NSInteger)pid name:(NSString *)name age:(NSInteger)age gender:(NSString *)gender {
    if (self = [super init]) {
        _pid = pid;
        _name = name;
        _age = age;
        _gender = gender;
    }
    return self;
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

#pragma mark - NSCoding 编码和解码(归档和反归档,压缩和解压缩)
// 归档 编码
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeInteger:_pid forKey:@"pid"];
    [aCoder encodeObject:_name forKey:@"name"];
    [aCoder encodeInteger:_age forKey:@"age"];
    [aCoder encodeObject:_gender forKey:@"gender"];
}

// 反归档 解码
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        _pid = [aDecoder decodeIntegerForKey:@"pid"];
        _name = [aDecoder decodeObjectForKey:@"name"];
        _age = [aDecoder decodeIntegerForKey:@"age"];
        _gender = [aDecoder decodeObjectForKey:@"gender"];
    }
    return self;
}
@end
//
//  ViewController.h
//  自定义对象数据持久化
//
//  Created by shimei on 7/29/16.
//  Copyright © 2016 Shimei. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
//
//  ViewController.m
//  自定义对象数据持久化
//
//  Created by shimei on 7/29/16.
//  Copyright © 2016 Shimei. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"

@interface ViewController ()
@property (copy,   nonatomic) NSString *filePath;
@end

@implementation ViewController
- (NSString *)filePath {
    if (!_filePath) {
        NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        _filePath = [docPath stringByAppendingPathComponent:@"person.plist"];
    }
    return _filePath;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"file path:%@",self.filePath);
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:self.filePath]) { // 文件已经存在,进行读取文件(反归档)
        NSLog(@"反归档");
        NSData *data = [NSData dataWithContentsOfFile:self.filePath];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        Person *aPerson = [unarchiver decodeObjectForKey:@"person"];
        [unarchiver finishDecoding];
        
        NSLog(@"pid:%ld, name:%@, age:%ld, gender:%@",aPerson.pid, aPerson.name, aPerson.age, aPerson.gender);
        
    } else { // 文件不存在,进行写入文件(归档)
        NSLog(@"归档");
        // 创建并实例化一个Person对象
        Person *person = [[Person alloc] initWithPid:1001 name:@"LSM" age:18 gender:@"m"];
        NSMutableData *mdata = [NSMutableData data];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mdata];
        [archiver encodeObject:person forKey:@"person"];
        [archiver finishEncoding];
        
        // 把数据写入到disk
        [mdata writeToFile:self.filePath atomically:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

相关文章

  • iOS持久化存储

    iOS有以下几种数据持久化方式 NSUserDefault (无法保存自定义对象) plist表 (无法保存自定义...

  • iOS 归档、NSCoding协议

    一、归档介绍 1.归档是将数据持久化的一种方式,一般针对于比较复杂对象,比如自定义的对象,来进行数据持久化操作。 ...

  • iOS归档,NSCoding协议

    一、归档介绍 1.归档是将数据持久化的一种方式,一般针对于比较复杂对象,比如自定义的对象,来进行数据持久化操作。 ...

  • 面试相关

    数据持久化 什么是持久化狭义的理解: “持久化”仅仅指把域对象永久保存到数据库中;广义的理解,“持久化”包括和数据...

  • 自定义对象数据持久化

  • 数据持久化

    什么是对象持久化 所谓持久化(Persistence)即把数据(如内存中的对象)保存到持久化的设备,即可永远保存到...

  • 设计模式之DAO模式

    简介 DAO (DataAccessobjects 数据存取对象)是指位于业务逻辑和持久化数据之间实现对持久化数据...

  • 对象持久化和数据序列化

    对象持久化(Persistence) 对象持久化就是将对象存储在可持久保存的存储介质上,例如主流的关系数据库中。 ...

  • mashibing的ddd

    贫血模型:固有行为:不用持久化: 数据对象模型中非固有能力:需要跟数据库进行持久化; 业务逻辑层 关联对象要能...

  • 一些名词解释

    持久化对象持久化就是将对象存储在可持久保存的存储介质上,例如主流的关系数据库中。在实际应用中,需要将业务数据以对象...

网友评论

      本文标题:自定义对象数据持久化

      本文链接:https://www.haomeiwen.com/subject/rblzjttx.html