/**
* 写入plist文件
*
* @param writecontext 要写入的内容
* @param writeName 要写入内容的对应键
*/
+(void)writeToPlist:(ZMPlistKey)zmPlistKey name:(NSString *)writecontext
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path =[paths objectAtIndex:0];
NSString *filename = [path stringByAppendingPathComponent:kPlistName]; //获取路径
//创建一个dic,写到plist文件
NSString *writeName = [self returnPlistKey:zmPlistKey];
if (writeName) {
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@",writecontext],writeName,nil]; //写入数据
[dic writeToFile:filename atomically:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:@"token" object:nil];
}
}
/**
* 读取plist文件
*
* @param key 对应的键
*
* @return 对应键的内容
*/
+(NSString *)readToPlist:(ZMPlistKey)zmPlistKey
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = [paths objectAtIndex:0];
NSString *filename = [path stringByAppendingPathComponent:kPlistName]; //获取路径
NSLog(@"%@",filename);
NSString *readName = [self returnPlistKey:zmPlistKey];
NSString *str = nil;
if (readName) {
NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename]; //读取数据
str = dic2[readName];
}
return str;
}
/**
* 将dict写入plist文件
*
* @param dict 要写入plist文件的dict
*/
+(void)dictWriteToPlist:(NSDictionary *)dict plist:(NSString *)plist
{
NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:plist];
NSLog(@"write %@",plistPath);
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dict
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
[plistData writeToFile:plistPath atomically:YES];
}
/**
* 读取dict的plist文件
*
* @param plist 读取plist文件
*/
+(NSDictionary *)dictReadPlist:(NSString *)plist
{
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:plist];
NSLog(@"read %@",plistPath);
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
return dictionary;
}