zoukankan      html  css  js  c++  java
  • 数据的归档和解归档

    数据的归档和解归档

    //归档

    BOOL success = [NSKeyedArchiver archiverRootObject:归档的对象 toFile:文件路径];

    //解档

    id content =  [NSKeyedUnArchiver unarchiveObjectWithFile:文件路径];

     

    //第二种解归档(多对象)

    //归档

    NSString *homePath = NSHomePath();

    NSString *filePath = [homePath stringByAppendingPathComponent:归档文件名];

    NSMutableData *data = [NSMutableData data];

    NSKeyedArchiver *archive = [[NSKeyedArchiver alloc]initForWritingMutableData:data];

    [archive encodeInt:100 forKey:@“age”];

    [archive encodeObject:对象名 forKey :@“name”];

    [archive finishEncode];

    [archive release];

    BOOL success = [data writeToFile :filePath atomically:YES ];

    //解档

    NSString *homePath = NSHomePath();

    NSString *filePath = [homePath stringByAppendingPathComponent:归档文件名];

    NSData *data = [NSData dataWithContentsOfFile:filePath];

    NSKeyedUnarchiver  *unarchive = [[NSKeyedUnarchiver alloc]initForReadingWithData:data]; 

    int num = [unarchive decodeIntForKey:@“age”];

    id obj = [unarchive decodeObjectForKey:@“name”];

    [unarchive release ];

    //自定义归档的时候要遵守NSEcoding协议才行

    //归档的时候 要实现方法

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

    //解档的时候要实现

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

    self = [super init];

    if  (self  != nil){

     }

    return self;

     }//注意点     对象属性的所有权

  • 相关阅读:
    const变量指针赋值给非const类型的指针运行结果
    嵌套结构可以访问外部结构的私有成员吗?
    几种cms的介绍
    中国互联网网站尴尬排行榜[转]
    如何跨域来同步不同网站之间的Cookie
    CMS整站程序整理
    vs 设置断点
    ASP.NET 4中的SEO改进
    VSS演示
    发生一个未处理的异常 脚本调试 错误号2912
  • 原文地址:https://www.cnblogs.com/meixian/p/5370986.html
Copyright © 2011-2022 走看看