博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
复杂对象写入文件
阅读量:4704 次
发布时间:2019-06-10

本文共 2894 字,大约阅读时间需要 9 分钟。

//简单对象可以通过直接写入文件的方式进行存储,复杂对象我们无法直接写入文件,这个时候需要借助归档和反归档

//归档和反归档并不是数据持久化的方式,而是将复杂对象转化成简单对象的一种方式

    Person * per = [Person new];

    per.name = @"欧阳冰";
    per.gender = @"神秘";
    per.hobby = @"美女";
    per.age = @"21";
    
    //准备一个路径
    NSString * path = NSHomeDirectory();
    path = [path stringByAppendingString:@"/曹江涛.avi"];
    NSLog(@"%@",path);
    //创建数据对象,用来存放person
    NSMutableData * data = [NSMutableData data];
    //创建归档对象
    NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //归档
    [archiver encodeObject:per forKey:@"secret"];
    //完成归档
    [archiver finishEncoding];
    //写入文件(writeToFile)
    [data writeToFile:path atomically:YES];
    
    //反归档
    NSData * _data = [NSData dataWithContentsOfFile:path];
    //创建反归档对象
    NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:_data];
    //反归档数据
    Person * per1 = [unarchiver decodeObjectForKey:@"secret"];
    //反归档完成
    [unarchiver finishDecoding];
    NSLog(@"%@",per1.name);

方法二:不用OC封装好的方法,序列化写入

 将复杂对象序列化:

eg.将UserListsetNode *node,类型的对象写入文件。

#import 
@interface UserListsetNode : NSObject
//序列化{@public NSString* m_pstrInfo;//如果服务器异常这边就是错误信息 NSArray *m_pUsersArray; NSMutableDictionary *m_pUserSetDic; NSString *m_pstrTime;}+(NSData*)Request:(NSString*)pstrToken ToId:(NSString*)pstrToId;-(BOOL) DecodeJson:(NSData*)pData;@end
#import "UserListsetNode.h"@implementation UserListsetNode+(NSData*)Request:(NSString *)pstrToken ToId:(NSString*)pstrToId{    NSString *pstrUrl = [NSString stringWithFormat:@"%@%@",BASEURL,@"user/listset/"];    NSString *pstrData = [NSString stringWithFormat:@"{\"token\":\"%@\",\"toid\":\"%@\"}",pstrToken,pstrToId];    return [HttpUtil Request:pstrUrl data:pstrData];}-(BOOL)DecodeJson:(NSData *)pData{    m_pstrInfo = @"";        if (nil == pData)    {        return NO;    }        NSError *pError;    NSDictionary *pRetDic = [NSJSONSerialization JSONObjectWithData:pData  options:NSJSONReadingMutableContainers error:&pError];        if (nil == pRetDic)    {        return NO;    }        if ([[pRetDic objectForKey:@"status"] integerValue] == 0)    {        m_pstrInfo = [pRetDic objectForKey:@"info"];        return NO;    }else{        NSArray *pArray = [[NSArray alloc]init];        if(nil == m_pUserSetDic)        {            m_pUserSetDic = [[NSMutableDictionary alloc]init];        }        pArray = [pRetDic objectForKey:@"users"];        for (NSDictionary *pDic in pArray) {            for (int i =0; i

使用时:

-(void)SetUserSettings:(UserListsetNode *)pUserSet{    if ([CommonUtil FileExist:DIR_USERLISTSET]) {    //将序列化的对象写入文件        [NSKeyedArchiver archiveRootObject:pUserSet toFile:DIR_USERLISTSET];        [self GetUserSettings];            }else{        [[NSFileManager defaultManager]createFileAtPath:DIR_USERLISTSET contents:nil attributes:nil];        [self SetUserSettings:pUserSet];    }}

 

 
 

转载于:https://www.cnblogs.com/yangqinglong/p/5479832.html

你可能感兴趣的文章
《构建之法》前三章的读后感
查看>>
12306
查看>>
Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结
查看>>
python newbie——PE No.9
查看>>
20165218 实验一 Java开发环境的熟悉
查看>>
map标签的详细使用参数
查看>>
MySQL对于有大量重复数据表的处理方法
查看>>
Android应用开发学习笔记之多线程与Handler消息处理机制
查看>>
ubuntu 设置环境变量
查看>>
JSTL详解(一)
查看>>
Manacher 算法
查看>>
Linux磁盘及文件系统(三)Linux文件系统
查看>>
SDWebImage源码阅读(二)NSData+ImageContentType
查看>>
别在最好的年纪辜负最好的自己
查看>>
年终总结
查看>>
==和equals区别
查看>>
Http协议
查看>>
flappy-bird
查看>>
侏罗纪世界
查看>>
kvm linux虚拟机在线扩展磁盘
查看>>