方法一:使用归档:其实质就是读写文件操作,(归档,解档:archive,unarchive)
// 归档
+ (NSData *)archivedDataWithRootObject:(id)rootObject;
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;
// 解档
+ (id)unarchiveObjectWithData:(NSData *)data;
+ (id)unarchiveObjectWithFile:(NSString *)path;
归档写入文件有两种:
1.一个文件只保存一个对象
此种情况就是上面的简单归档,解档,如上两段代码。
2.一个文件保存多个对象
此种情况就是相当于写文件操作,
具体看以下代码:
-(void)saveDoctorInfo:(DoctorModel *)doctor;
-(DoctorModel *)getDoctorInfo:(NSString *)accid;
注:
NSKeyedArchiver *archiver = [[NSKeyedArchiveralloc] initForWritingWithMutableData:data];
/ /创建NSKeyedArchiver实例,用于将对象归档到此theData实例中。
一个文件保存多个用户带来的问题有:
(1.)可以按顺序存储用户数据,但是不好限制保存个数(假如限制保存三个用户信息)
对于文件中的内容操作,只有读写,但是如果保存的用户越来越多,文件会越来越大。
如果要删除文件中的数据(字符串)只能用指针来实现。
第一种是用空字符串覆盖掉你所需要修改的数据。第二种是找到初始指针,往后一直写,数据便会覆盖掉。但是所要删除的对象数据结束指针位置难以知道。如果要删除的对象是第二个,则很有可能会把第三个用户信息覆盖掉。
(2.)无法按顺序取出保存的用户:
因为存储,查询文件中的对象使用的方法为:
- (void)encodeObject:(id)objv forKey:(NSString *)key;
- (id)decodeObjectForKey:(NSString *)key;
相当于文件中存储对象的方式是字典类型的,无法像数组那样获得顺序
//
// DoctorManager.m
// com.yx129.yxClientDoctor3
//
// Created by yx on 15/2/11.
// Copyright (c) 2015年 Guangzhou Yixiang Internet Technology Development Limited. All rights reserved.
//
#define fileName_LastDoctorInfo @"lastDoctor.info"
#define fileName_SavedDoctorInfo @"savedDoctor.info"
//#define klastDoctorEncodeKey @"lastDoctor"
#define ksavedDoctorEncodeKey @"savedDoctor"
#import "DoctorManager.h"
@implementation DoctorManager
{
// NSMutableDictionary * _lastDoctorInfoDic;
// NSString *_lastDoctorInfoPath;
}
static DoctorManager * manager=nil;
static DoctorModel *lastDoctor = nil;
static DoctorModel *toolDoctor = nil;
static DoctorModel *savedDoctor = nil;
+(id)shareDoctorManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager=[[DoctorManager alloc]init];
toolDoctor = [[DoctorModel alloc] init];
});
return manager;
}
+(DoctorModel *)getSavedDoctor
{
if (savedDoctor == nil){
savedDoctor = [[DoctorModel alloc] init];
}
return savedDoctor;
}
-(id)init
{
if (self=[super init]) {
// _lastDoctorInfoPath=[NSString stringWithFormat:@"%@/Documents/lastDoctorInfo.plist",NSHomeDirectory()];
}
return self;
}
-(void)saveLastLoginDoctor:(DoctorModel *)doctor
{
// NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
// NSString *path=[docPath stringByAppendingPathComponent:fileName_LastDoctorInfo];
NSString *path =[self getFilePath:fileName_LastDoctorInfo];
[NSKeyedArchiver archiveRootObject:doctor toFile:fileName_LastDoctorInfo];
}
-(DoctorModel *)getLastLoginDoctor
{
if (!lastDoctor) {
lastDoctor=[[DoctorModel alloc]init];
}
// NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// NSString *path=[docPath stringByAppendingPathComponent:fileName_LastDoctorInfo];
NSString *path =[self getFilePath:fileName_LastDoctorInfo];
lastDoctor = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
return lastDoctor;
}
#pragma 保存账户信息
-(void)saveDoctorInfo:(DoctorModel *)doctor
{
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:doctor forKey:doctor.acc_id];
[archiver finishEncoding];
[data writeToFile:[self getFilePath:fileName_SavedDoctorInfo] atomically:YES];
}
-(DoctorModel *)getDoctorInfo:(NSString *)accid
{
if ([[NSFileManager defaultManager] fileExistsAtPath:[self getFilePath:fileName_SavedDoctorInfo]]) {
NSData *data = [[NSData alloc] initWithContentsOfFile:[self getFilePath:fileName_SavedDoctorInfo]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
if (!lastDoctor) {
lastDoctor=[[DoctorModel alloc]init];
}
lastDoctor = [unarchiver decodeObjectForKey:accid];
[unarchiver finishDecoding];
}
return lastDoctor;
}
-(DoctorModel *)getLastSavedDoctorInfo
{
DoctorModel *lastSavedDoctor=[[DoctorModel alloc]init];
lastSavedDoctor = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getFilePath:fileName_LastDoctorInfo]];
return lastSavedDoctor;
}
-(NSString *)getFilePath:(NSString *)fileName
{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];
}
/*
-(void)saveLastLoginDoctor:(DoctorModel *)doctor
{
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"lastDoctor.info"];
NSLog(@"path=%@",path);
toolDoctor = doctor;
[NSKeyedArchiver archiveRootObject:toolDoctor toFile:path];
}
-(DoctorModel *)getLastLoginDoctor
{
if (!lastDoctor) {
lastDoctor=[[DoctorModel alloc]init]; // 设置状态,是否已经进入信息详情页面
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path);
toolDoctor = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}
lastDoctor = toolDoctor;
// if (!lastDoctor) {
// lastDoctor=[[DoctorModel alloc]init]; // 设置状态,是否已经进入信息详情页面
//
// NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
// NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
// NSLog(@"path=%@",path);
//
// lastDoctor = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// }
return lastDoctor;
}
//-(void)synchronize
//{
// [_lastDoctorInfoDic writeToFile:_lastDoctorInfoPath atomically:YES];
//
// [_lastDoctorInfoDic removeAllObjects];
//
//}
*/
@end
由于以上原因,当你需要增删查改多个对象时,不能采用直接保存多个对象形式,而要将对象用数组包装,再将这个数组写入归档。
这时候根对象是数组。
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;
-(void)saveDoctor:(DoctorModel *)doctor
{
NSString *path =[self getFilePath:fileName_SavedDoctorInfo];
NSMutableArray *doctorsArr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (doctorsArr == nil) {
doctorsArr = [NSMutableArray arrayWithObject:doctor];
}else{
if (doctorsArr.count >= 5)
[doctorsArr removeObjectAtIndex:0];
[doctorsArr addObject:doctor];
}
[NSKeyedArchiver archiveRootObject:doctorsArr toFile:fileName_SavedDoctorInfo];
}
-(DoctorModel *)getDoctor:(NSString *)accid
{
NSString *path =[self getFilePath:fileName_SavedDoctorInfo];
NSMutableArray *doctorsArr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
DoctorModel *aDoctor;
if (doctorsArr != nil) {
for (DoctorModel *savedDoc in doctorsArr) {
if ([savedDoc.acc_id isEqualToString:accid]) {
aDoctor = savedDoc;
break;
}
}
}
return aDoctor;
}
-(NSString *)getFilePath:(NSString *)fileName
{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];
}
方法二:用plist
需要用将数据加密后再进行存储
md5 只能 加密不能解密
应用 aes 加密算法
此种方法,排序是个问题