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

自定义对象数据持久化

作者: 景彧 | 来源:发表于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
    

    相关文章

      网友评论

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

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