Skip to content

Instantly share code, notes, and snippets.

@xuyunan
Last active December 10, 2015 14:58
Show Gist options
  • Save xuyunan/4450810 to your computer and use it in GitHub Desktop.
Save xuyunan/4450810 to your computer and use it in GitHub Desktop.
Plist文件操作
/*
先在项目中加好默认的设置,读取的时候如果没有就把默认的写入。
*/
#define FILE_NAME @"CONFIG"
#define FILE_TYPE @"plist"
// eg: cName shanghai
+ (void)setDefultCity:(NSString *)cName
{
NSDictionary *dic = [CITYSManager getCityPlist];
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:dic];
NSString *defaultCity = [mutableDic objectForKey:@"default"];
if ([defaultCity isEqualToString:cName]) {
return;
} else {
[mutableDic setObject:cName forKey:@"default"];
[mutableDic writeToFile:[CITYSManager pathFileDocument] atomically:YES];
}
}
+ (NSDictionary *)getDefaultCity
{
NSDictionary *rootDic = [CITYSManager getCityPlist];
NSString *defaultKey = [rootDic objectForKey:@"default"];
return [rootDic objectForKey:defaultKey];
}
- (NSDictionary *)readConfigPlistFile
{
NSString *filePath = [self pathFileDocument];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
return dic;
}else {
NSString *path = [[NSBundle mainBundle] pathForResource:FILE_NAME ofType:FILE_TYPE];
NSDictionary *dicTemp = [[NSDictionary alloc] initWithContentsOfFile:path];
[dicTemp writeToFile:filePath atomically:YES];
NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
return dic;
}
}
- (NSString *)pathFileDocument
{
NSArray *arrayFiles = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [arrayFiles objectAtIndex:0];
NSString *filePath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",FILE_NAME,FILE_TYPE]];
return filePath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment