• 数据持久化------Archiving(归档,解档)


    其中TRPerson为自定义的继承自NSObject的类的子类  其中有两个属性,name 和 age

    .h文件

    #import

    @interface TRPerson : NSObject<</span>NSCoding>

    @property (nonatomic,strong)NSString *name;

    @property (nonatomic,strong)NSNumber * age;

    //初始化方法

    - (id)initWithName:(NSString*)name withAge:(NSNumber *)age;

    @end

    .m文件

    #import "TRPerson.h"

    @implementation TRPerson

    - (id)initWithName:(NSString *)name withAge:(NSNumber *)age{

        if (self=[super init]) {

            self.age=age;

            self.name=name;

        }

        return  self;

        

    }

    #pragma mark - NSCoding

     //对属性进行解码(时机:执行encodeObject方法)

    - (instancetype)initWithCoder:(NSCoder *)aDecoder{

       self= [super init];

        if (self) {

            self.name= [aDecoder decodeObjectForKey:@"name"];

            self.age=[aDecoder decodeObjectForKey:@"age"];

        }

        return self;

    }

    //对属性进行编码的方法

    - (void)encodeWithCoder:(NSCoder *)aCoder{

        [aCoder encodeObject:self.age forKey:@"age"];

        [aCoder encodeObject:self.name forKey:@"name"];

        

    }

    - (NSString *)description{

        TRPerson *person=[[TRPerson alloc]initWithName:self.name withAge:self.age];

        return  [NSString stringWithFormat:@"name:%@ age:%@",person.name ,person.age];

    }

    @end

    ViewController中的viewDidLoad方法中实现数据的归档和解挡

     - (void)viewDidLoad {

        [super viewDidLoad];

        //准备工作

         //将自定义的TRPerson对象进行归档(写)

        

        TRPerson *person=[[TRPerson alloc]initWithName:@"张飞" withAge:@20];

        //Documents/archiving

        NSString *doumentsPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject;

        NSString *archiverPath = [doumentsPath stringByAppendingPathComponent:@"archving"];

        //1.可变数据类型

        NSMutableData *data=[NSMutableData data];

        //2.归档对象

        NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

        //3.编码

        

        [archiver encodeObject:person forKey:@"person"];

        //4.编码完成

        [archiver finishEncoding];

        //5.写入文件

        

        [data writeToFile:archiverPath atomically:YES];

        //将自定义的TRPerson对象进行解挡(读)

        //1.读取数据

        NSData *readingData=[NSData dataWithContentsOfFile:archiverPath];

        //2.解码对象

        NSKeyedUnarchiver *unArchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:readingData];

        //3.解码

     TRPerson *personRead=[unArchiver decodeObjectForKey:@"person"];

        //4.完成解码

        [unArchiver finishDecoding];

        //验证

        NSLog(@"%@",personRead );

    }

    追寻最真
  • 相关阅读:
    Class的用途
    Flash网络编程安全沙箱问题隆重解决 (转)
    带参数的EventDispatcher
    Object的效率
    Oracle数据库语言修改成UTF8
    Python之字符串详解1
    初级/中级/高级运维,你是哪一级?
    这可能是php世界中最好的日志库——monolog
    vc程序大小优化最佳方案(转)http://blog.sina.com.cn/s/blog_4c50333c0100gjs3.html
    C# 调用lua 报错未能加载文件或程序集“lua51.dll”或它的某一个依赖项。找不到指定的模块。 解决方法
  • 原文地址:https://www.cnblogs.com/zhao-jie-li/p/5128460.html
Copyright © 2020-2023  润新知