iOS 三方库·自定义控件·源码解析iOS技术点

快速生成plist文件

2017-01-13  本文已影响36人  邹邹_ZZ
很多时候把数据存到plist文件里,用的时候去取不失为一种简洁方便的好办法,那下面我们就学习下如何快速生成plist文件吧。
+(void)writePlistFilesOfresourceName:(NSString*)resorceName resourceType:(NSString*)type  PlistName:(NSString*)plistName{
//获取json   文件
NSString *txtPath = [[NSBundle mainBundle] pathForResource:resorceName ofType:type];
NSData *jsonData = [NSData dataWithContentsOfFile:txtPath options:NSDataReadingMappedIfSafe error:nil];
NSMutableDictionary *nameDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

//获取本地沙盒路径
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//获取完整路径
NSString *documentsPath = [path objectAtIndex:0];
NSString *resultPath = [documentsPath stringByAppendingPathComponent:[NSString  stringWithFormat:@"%@.plist",plistName]];
NSLog(@"resultPath:\n%@",resultPath);

//写入文件
[nameDict writeToFile:resultPath atomically:YES];  }
上一篇下一篇

猜你喜欢

热点阅读