本地存取方法集成

2016-11-08  本文已影响30人  PZcoder

目录:
1、保存数据到本地
2、读取本地数据
3、更新本地数据
4、清空本地指定数据

1、保存数据到本地

/*
*存储数据到plist、userDefault、db
*
*@param dataDic
*  要存储的数据,即将要存储的数据拼装成NSMutableDictionary,将其传入方法
*
*@param cacheType
*类型有三种:DataModePlist:plist,
DataModeUserDefault:userDefault,
DataModeDB:db
*
*@param name
*  为自己要存储的数据起个名称,以后方便找到
*
*@调用举例
*   NSMutableDictionary* dict = [[NSMutableDictionary alloc]  initWithObjectsAndKeys:@"a",@"A",@"b",@"B",@"c",@"C" nil];
[StaticTools saveData:dict cacheType:DataModePlist dataName:@"test"];
*
*@返回值 BOOL
*                  YES:成功
NO:失败
*
*@说明
*   如果存入db,则该数据字典每个元素必须是nsstring,并且与所建表中个数相同,如建表时有A,B,C三列,只希望A,C列有值,则B列传入@“”
eg:
NSMutableDictionary* dict = [[NSMutableDictionary alloc]  initWithObjectsAndKeys:@"a",@"A",@"",@"B",@"c",@"C" nil];
[StaticTools saveData:dict cacheType:DataModePlist dataName:@"test"];
*/
+ (BOOL)saveData:(NSMutableDictionary *)dataDic cacheType:(DataMode)cacheType dataName:(NSString*)name
{
   if ([dataDic count] == 0 || !dataDic)
   {
       return NO;    
   }
   
   //Plist
   if (cacheType == DataModePlist)
  {
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *documentsDirectory = [paths objectAtIndex:0];
       NSString *path_ = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",name]];
       NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path_];

       if (!tempDictionary)
       {
           tempDictionary = [NSMutableDictionary dictionaryWithCapacity:0];
       }

       [tempDictionary setObject:dataDic forKey:name];
       [tempDictionary writeToFile:path_ atomically:YES];

       return YES;
   }
   //UserDefault
   else if (cacheType ==DataModeUserDefault)
   {
       NSUserDefaults* muDefault = [NSUserDefaults standardUserDefaults];
       [muDefault setObject:dataDic forKey:name];

       return YES;
   }
   
   //DB
   //    else if (cacheType ==DataModeDB){
   //        DBSqlite* dbt = [[[DBSqlite alloc] init] autorelease];
   //        return [dbt insert2DB:dataDic tableName:name];
   //    }
   
   //其他
   else{
       NSLog(@"SaveDataMode Error");
       return NO;
   }
}

2、读取本地数据

/*
 *从plist、userDefault、db中读取数据
 *
 *@param cacheType
 *  类型有三种:DataModePlist:plist,
 DataModeUserDefault:userDefault,
 DataModeDB:db
 *
 *@param name
 *  用存储时的名字读取
 *
 *@调用举例
 *    [StaticTools readData:DataModePlist dataName:@"test"];
 *
 *@返回值 BOOL
 *                  YES:成功
 NO:失败
 *
 *@说明
 *   若读取DB,返回该表中所有的记录,类型为NSArray,数组中每条为一个NSMutableDictionary,为了和plist和userDefault统一,方法整体返回为NSMutableDictionary,用参数name的值取得数组
 */

+(NSMutableDictionary*)readData:(DataMode)cacheType dataName:(NSString*)name
{
    //for analyze
    NSMutableDictionary *cacheDict = [NSMutableDictionary dictionaryWithCapacity:0];

    if ([name length] == 0)
        return cacheDict;
    
    //Plist
    if (cacheType == DataModePlist) 
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path_ = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",name]];

        if (![[NSFileManager defaultManager] fileExistsAtPath:path_]) 
        {
            [cacheDict writeToFile:path_ atomically:YES];
        }
        else
        {
            //for analyze
            //            NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path_];
            NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path_];
            cacheDict = [tempDictionary objectForKey:name];
        }
    }

    //UserDefault
    else if (cacheType ==DataModeUserDefault)
    {
        NSUserDefaults* muDefault = [NSUserDefaults standardUserDefaults];
        cacheDict = [muDefault objectForKey:name];
        NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:cacheDict];

        return dict;
    }
    
    //DB
    else if (cacheType ==DataModeDB)
    {
        //        DBSqlite* dbt = [[DBSqlite alloc] init];
        //        [cacheDict setObject:[dbt readDB:name] forKey:name];
        //        [dbt release];
    }
    
    //其他
    else
    {
        NSLog(@"ReadDataMode Error");
    }

    return cacheDict;
}

3、更新本地存储的数据

/*
 *从plist、userDefault、db中更新数据
 *
 *@param newDataDic
 *  想要更新的数据,类型为NSMutableDictionary
 *
 *@param cacheType
 *  类型有三种:DataModePlist:plist,
 DataModeUserDefault:userDefault,
 DataModeDB:db
 *
 *@param name
 *  用存储时的名字更新
 *
 *@param dbWhere
 *  更新条件,类型为NSMutableDictionary
 *
 *@返回值 BOOL
 *                  YES:成功
 NO:失败
 *
 *@调用举例
 *    [StaticTools updateData:new cacheType:DataModePlist name:@"test" DBWhere:nil];
 *
 *@说明
 *   若更新plist或userDefault,则dbWhere参数传入nil
 eg: NSMutableDictionary* new = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"e",@"A", nil];
 [StaticTools updateData:new cacheType:DataModePlist name:@"test" DBWhere:nil];
 若为db
 eg:    NSMutableDictionary* new = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"e",@"A", nil];
 NSMutableDictionary* where = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"b",@"B", nil];
 [StaticTools updateData:new cacheType:DataModeDB name:@"test" DBWhere:where];
 */

+ (BOOL)updateData:(NSMutableDictionary *)newDataDic cacheType:(DataMode)cacheType name:(NSString*)name DBWhere:(NSMutableDictionary*)dbWhere
{
    if ([newDataDic count] == 0 || !newDataDic)
    {
        return NO;
    }
    
    //Plist
    if (cacheType == DataModePlist) 
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *DocumentsPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",name]];

        if ([[NSFileManager defaultManager] fileExistsAtPath:DocumentsPath]) 
        {
            [[NSFileManager defaultManager] removeItemAtPath:DocumentsPath error:nil];
        }
        
        NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:DocumentsPath];

        if(!tempDictionary)
        {
            tempDictionary = [[NSMutableDictionary alloc] init];
        }

        [tempDictionary setObject:newDataDic forKey:name];
        [tempDictionary writeToFile:DocumentsPath atomically:YES];

        return YES;
    }
    
    //UserDefault
    else if (cacheType ==DataModeUserDefault)
    {
        NSUserDefaults* muDefault = [NSUserDefaults standardUserDefaults];
        [muDefault setObject:newDataDic forKey:name];

        return YES;
    }
    
    //DB
    //    else if (cacheType ==DataModeDB){
    //        DBSqlite* dbt = [[[DBSqlite alloc] init] autorelease];
    //        return [dbt updateData:newDataDic name:name DBWhere:dbWhere];
    //    }
    
    //其他
    else
    {
        NSLog(@"SaveDataMode Error");
        return NO;
    }
}

4、清空本地指定数据

/*
 *清空plist、userDefault、db中指定名称的数据
 *
 *@param cacheType
 *  类型有三种:DataModePlist:plist,
 DataModeUserDefault:userDefault,
 DataModeDB:db
 *
 *@param name
 *  要删除数据的名称,即存储时传入的名称
 *
 *@返回值 BOOL
 *                  YES:成功
 NO:失败
 *
 *@调用举例
 *   [StaticTools clearData:DataModePlist dataName:@"test"];
 */

+(BOOL)clearData:(DataMode)cacheType dataName:(NSString*)name
{
    //Plist
    if (cacheType == DataModePlist)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *DocumentsPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",name]];

        if ([[NSFileManager defaultManager] fileExistsAtPath:DocumentsPath])
        {
            [[NSFileManager defaultManager] removeItemAtPath:DocumentsPath error:nil];
        }

        return YES;
    }
    
    //UserDefault
    else if (cacheType ==DataModeUserDefault)
    {
        NSUserDefaults* muDefault = [NSUserDefaults standardUserDefaults];
        [muDefault removeObjectForKey:name];

        return YES;
    }
    
    //DB
    //    else if (cacheType ==DataModeDB){
    //        DBSqlite* dbt = [[[DBSqlite alloc] init] autorelease];
    //        return [dbt clearData:name];
    //    }
    
    //其他
    else
    {
        NSLog(@"ReadDataMode Error");

        return NO;
    }
}
上一篇下一篇

猜你喜欢

热点阅读