iOS 轻量存储

也可以根据自己的需求进行模型字典的存储


.h

//

//  ZY_DataSaver.h

//  testTime

//

//  Created by 飞奔的羊 on 16/12/14.

//  Copyright © 2016年 Beijing Zhianyi Co, Ltd. All rights reserved.

//

#import

@interfaceZY_DataSaver :NSObject

+(void)saveChangeInfo:(NSDictionary*)dic;

//轻量存储

+(void)saveString:(NSString*)string forKey:(NSString*)key;

//轻量获取本地数据

+(NSString*)getStringForKey:(NSString*)key;

//轻量存储

+(void)saveNSDictionary:(NSDictionary*)dic forKey:(NSString*)key;

//轻量获取本地数据

+(NSDictionary*)getNSDictionaryForKey:(NSString*)key;

//轻量存储

+(void)saveArray:(NSArray*)array forKey:(NSString*)key;

//轻量获取本地数据

+(NSArray*)getArrayForKey:(NSString*)key;

//删除本地轻量数据

+(void)removeStringForKey:(NSString*)key;

@end


.m

#import "ZY_DataSaver.h"

//#import "ZYUserInfoMod.h"

@implementation ZY_DataSaver

+(void)saveChangeInfo:(NSDictionary*)dic

{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) ; //得到documents的路径,为当前应用程序独享


    NSString*documentD = [pathsobjectAtIndex:0];



    NSString*configFile = [documentDstringByAppendingPathComponent:@"ChangeInfo.plist"];//得到documents目录下dujw.plist配置文件的路径





    NSMutableDictionary *configList =[[NSMutableDictionary alloc] initWithContentsOfFile:configFile];  //初始/Users/zyahs/Desktop/test 合集/并夕夕/并夕夕Tests/Info.plist化字典,读取配置文件的信息

    //    NSMutableDictionary *configList =[NSMutableDictionary dictionaryWithContentsOfFile:configFile];



    //第二:写入文件file

    if(!configList) {          //第一次,文件没有创建,因此要创建文件,并写入相应的初始值。


        configList = [[NSMutableDictionaryalloc]initWithDictionary:@{@"key":@"value"}];


        [configListwriteToFile:configFileatomically:YES];


    }



    BOOLlay = [dicwriteToFile:configFileatomically:YES];

    if(lay) {

        NSLog(@"写入成功");

    }else{

        NSLog(@"写入失败");


    }


}

+(void)saveString:(NSString*)string forKey:(NSString*)key

{

    //    [self removeStringForKey:key];

    //    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

    //    [defaults setObject:string forKey:key];

    //    [defaults synchronize];

}

+(NSString*)getStringForKey:(NSString*)key

{

    //    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

    return nil;

}

+(void)saveArray:(NSArray*)array forKey:(NSString*)key

{

    //    [self removeStringForKey:key];

    //    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

    //    [defaults setObject:array forKey:key];

    //    [defaults synchronize];


}

+(NSArray*)getArrayForKey:(NSString*)key

{

    //    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

    return nil;


}

+ (void)saveNSDictionary:(NSDictionary*)dic forKey:(NSString*)key

{

    //    [self removeStringForKey:key];

    //    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

    //    [defaults setObject:dic forKey:key];

    //    [defaults synchronize];


}

+(NSDictionary*)getNSDictionaryForKey:(NSString*)key

{


    //    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

    return nil;


}

你可能感兴趣的:(iOS 轻量存储)