登錄界面多個賬戶信息存儲問題


                  



方法一:使用歸檔:其實質就是讀寫文件操作,(歸檔,解檔: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 加密算法

此種方法,排序是個問題

























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章