//简单对象可以通过直接写入文件的方式进行存储,复杂对象我们无法直接写入文件,这个时候需要借助归档和反归档
//归档和反归档并不是数据持久化的方式,而是将复杂对象转化成简单对象的一种方式
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 <Foundation/Foundation.h> @interface UserListsetNode : NSObject <NSCoding>//序列化 { @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<pDic.allKeys.count; i++) { NSString* pKey = [[pDic allKeys] objectAtIndex:i] ; [m_pUserSetDic setValue:[pDic objectForKey:pKey] forKey:pKey]; } } m_pstrTime = [pRetDic objectForKey:@"time"]; } return YES; } //序列化代码 -(void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:m_pstrInfo forKey:@"info"]; [aCoder encodeObject:m_pUsersArray forKey:@"users"]; [aCoder encodeObject:m_pUserSetDic forKey:@"set"]; [aCoder encodeObject:m_pstrTime forKey:@"time"]; } -(id)initWithCoder:(NSCoder *)aDecoder{ m_pstrInfo = [aDecoder decodeObjectForKey:@"info"]; m_pUsersArray =[aDecoder decodeObjectForKey:@"users"]; m_pstrTime = [aDecoder decodeObjectForKey:@"time"]; m_pUserSetDic = [aDecoder decodeObjectForKey:@"set"]; return self; } @end
使用时:
-(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]; } }